source: mainline/uspace/app/edit/search.c@ 2f136e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2f136e4 was 7feb86e6, checked in by Martin Sucha <sucha14@…>, 13 years ago

Improve edit.

  • Fix Home moving caret to the end of previous line
  • Add next/prev char movement to spt
  • Refactor caret movement code
  • Implement search function
  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[7feb86e6]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 <errno.h>
39
40#include "search.h"
41#include "search_impl.h"
42
43search_t *search_init(const char *pattern, void *client_data, search_ops_t ops)
44{
45 search_t *search = calloc(1, sizeof(search_t));
46 if (search == NULL)
47 return NULL;
48
49 search->pattern = str_to_awstr(pattern);
50 if (search->pattern == NULL) {
51 free(search);
52 return NULL;
53 }
54
55 search->client_data = client_data;
56 search->ops = ops;
57 search->pattern_length = wstr_length(search->pattern);
58 search->back_table = calloc(search->pattern_length, sizeof(ssize_t));
59 if (search->back_table == NULL) {
60 free(search->pattern);
61 free(search);
62 return NULL;
63 }
64
65 search->pattern_pos = 0;
66
67 search->back_table[0] = -1;
68 search->back_table[1] = 0;
69 size_t table_idx = 2;
70 size_t pattern_idx = 0;
71 while (table_idx < search->pattern_length) {
72 if (ops.equals(search->pattern[table_idx - 1],
73 search->pattern[pattern_idx])) {
74 pattern_idx++;
75 search->back_table[table_idx] = pattern_idx;
76 table_idx++;
77 }
78 else if (pattern_idx > 0) {
79 pattern_idx = search->back_table[pattern_idx];
80 }
81 else {
82 pattern_idx = 0;
83 table_idx++;
84 }
85 }
86
87 return search;
88}
89
90int search_next_match(search_t *s, match_t *match)
91{
92 search_equals_fn eq = s->ops.equals;
93
94 wchar_t cur_char;
95 int rc = EOK;
96 while ((rc = s->ops.producer(s->client_data, &cur_char)) == EOK && cur_char > 0) {
97 /* Deal with mismatches */
98 while (s->pattern_pos > 0 && !eq(cur_char, s->pattern[s->pattern_pos])) {
99 s->pattern_pos = s->back_table[s->pattern_pos];
100 }
101 /* Check if the character matched */
102 if (eq(cur_char, s->pattern[s->pattern_pos])) {
103 s->pattern_pos++;
104 if (s->pattern_pos == s->pattern_length) {
105 s->pattern_pos = s->back_table[s->pattern_pos];
106 rc = s->ops.mark(s->client_data, &match->end);
107 if (rc != EOK)
108 return rc;
109 match->length = s->pattern_length;
110 return EOK;
111 }
112 }
113 }
114
115 match->end = NULL;
116 match->length = 0;
117
118 return rc;
119}
120
121void search_fini(search_t *search)
122{
123 free(search->pattern);
124 free(search->back_table);
125
126}
127
128bool char_exact_equals(const wchar_t a, const wchar_t b)
129{
130 return a == b;
131}
132
133/** @}
134 */
Note: See TracBrowser for help on using the repository browser.