source: mainline/uspace/dist/src/c/demos/edit/search.c@ f1380b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f1380b7 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2012 Martin Sucha
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup edit
30 * @{
31 */
32/**
33 * @file
34 * @brief Simple searching facility.
35 */
36
37#include <stdlib.h>
38#include <stddef.h>
39#include <errno.h>
40
41#include "search.h"
42#include "search_impl.h"
43
44search_t *search_init(const char *pattern, void *client_data, search_ops_t ops,
45 bool reverse)
46{
47 search_t *search = calloc(1, sizeof(search_t));
48 if (search == NULL)
49 return NULL;
50
51 wchar_t *p = str_to_awstr(pattern);
52 if (p == NULL) {
53 free(search);
54 return NULL;
55 }
56
57 search->pattern_length = wstr_length(p);
58
59 if (reverse) {
60 /* Reverse the pattern */
61 size_t pos, half;
62 half = search->pattern_length / 2;
63 for (pos = 0; pos < half; pos++) {
64 wchar_t tmp = p[pos];
65 p[pos] = p[search->pattern_length - pos - 1];
66 p[search->pattern_length - pos - 1] = tmp;
67 }
68 }
69
70 search->pattern = p;
71
72 search->client_data = client_data;
73 search->ops = ops;
74 search->back_table = calloc(search->pattern_length, sizeof(ssize_t));
75 if (search->back_table == NULL) {
76 free(search->pattern);
77 free(search);
78 return NULL;
79 }
80
81 search->pattern_pos = 0;
82
83 search->back_table[0] = -1;
84 search->back_table[1] = 0;
85 size_t table_idx = 2;
86 size_t pattern_idx = 0;
87 while (table_idx < search->pattern_length) {
88 if (ops.equals(search->pattern[table_idx - 1],
89 search->pattern[pattern_idx])) {
90 pattern_idx++;
91 search->back_table[table_idx] = pattern_idx;
92 table_idx++;
93 }
94 else if (pattern_idx > 0) {
95 pattern_idx = search->back_table[pattern_idx];
96 }
97 else {
98 pattern_idx = 0;
99 table_idx++;
100 }
101 }
102
103 return search;
104}
105
106errno_t search_next_match(search_t *s, match_t *match)
107{
108 search_equals_fn eq = s->ops.equals;
109
110 wchar_t cur_char;
111 errno_t rc = EOK;
112 while ((rc = s->ops.producer(s->client_data, &cur_char)) == EOK && cur_char > 0) {
113 /* Deal with mismatches */
114 while (s->pattern_pos > 0 && !eq(cur_char, s->pattern[s->pattern_pos])) {
115 s->pattern_pos = s->back_table[s->pattern_pos];
116 }
117 /* Check if the character matched */
118 if (eq(cur_char, s->pattern[s->pattern_pos])) {
119 s->pattern_pos++;
120 if (s->pattern_pos == s->pattern_length) {
121 s->pattern_pos = s->back_table[s->pattern_pos];
122 rc = s->ops.mark(s->client_data, &match->end);
123 if (rc != EOK)
124 return rc;
125 match->length = s->pattern_length;
126 return EOK;
127 }
128 }
129 }
130
131 match->end = NULL;
132 match->length = 0;
133
134 return rc;
135}
136
137void search_fini(search_t *search)
138{
139 free(search->pattern);
140 free(search->back_table);
141
142}
143
144bool char_exact_equals(const wchar_t a, const wchar_t b)
145{
146 return a == b;
147}
148
149/** @}
150 */
Note: See TracBrowser for help on using the repository browser.