mirror of
https://github.com/netsurf-browser/netsurf
synced 2024-12-22 12:12:35 +03:00
Remove forward declaration.
This commit is contained in:
parent
19b3876ff2
commit
119b5cca3f
614
render/search.c
614
render/search.c
@ -75,21 +75,6 @@ struct search_context {
|
||||
struct list_entry *current; /* first for select all */
|
||||
};
|
||||
|
||||
static void search_text(const char *string, int string_len,
|
||||
struct search_context *context, search_flags_t flags);
|
||||
static const char *find_pattern(const char *string, int s_len,
|
||||
const char *pattern, int p_len, bool case_sens,
|
||||
unsigned int *m_len);
|
||||
static bool find_occurrences_html(const char *pattern, int p_len,
|
||||
struct box *cur, bool case_sens,
|
||||
struct search_context *context);
|
||||
static bool find_occurrences_text(const char *pattern, int p_len,
|
||||
struct content *c, bool case_sens,
|
||||
struct search_context *context);
|
||||
static struct list_entry *add_entry(unsigned start_idx, unsigned end_idx,
|
||||
struct search_context *context);
|
||||
static void free_matches(struct search_context *context);
|
||||
|
||||
|
||||
/**
|
||||
* Find the browser window that contains the content associated with a search
|
||||
@ -174,58 +159,12 @@ struct search_context * search_create_context(hlcache_handle *h,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Begins/continues the search process
|
||||
* Note that this may be called many times for a single search.
|
||||
*
|
||||
* \param bw the browser_window to search in
|
||||
* \param flags the flags forward/back etc
|
||||
* \param string the string to match
|
||||
*/
|
||||
|
||||
void search_step(struct search_context *context, search_flags_t flags,
|
||||
const char *string)
|
||||
{
|
||||
int string_len;
|
||||
int i = 0;
|
||||
|
||||
if ((context == NULL) || (context->callbacks == NULL)) {
|
||||
warn_user("SearchError", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (context->callbacks->add_recent != NULL)
|
||||
context->callbacks->add_recent(string, context->p);
|
||||
|
||||
string_len = strlen(string);
|
||||
for(i = 0; i < string_len; i++)
|
||||
if (string[i] != '#' && string[i] != '*') break;
|
||||
if (i >= string_len) {
|
||||
union content_msg_data msg_data;
|
||||
free_matches(context);
|
||||
if (context->callbacks->status != NULL)
|
||||
context->callbacks->status(true, context->p);
|
||||
if (context->callbacks->back_state != NULL)
|
||||
context->callbacks->back_state(false, context->p);
|
||||
if (context->callbacks->forward_state != NULL)
|
||||
context->callbacks->forward_state(false, context->p);
|
||||
|
||||
msg_data.scroll.area = false;
|
||||
msg_data.scroll.x0 = 0;
|
||||
msg_data.scroll.y0 = 0;
|
||||
content_broadcast(context->c, CONTENT_MSG_SCROLL, msg_data);
|
||||
return;
|
||||
}
|
||||
search_text(string, string_len, context, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the memory used by the list of matches,
|
||||
* deleting selection objects too
|
||||
*/
|
||||
|
||||
void free_matches(struct search_context *context)
|
||||
static void free_matches(struct search_context *context)
|
||||
{
|
||||
struct list_entry *a;
|
||||
struct list_entry *b;
|
||||
@ -249,13 +188,278 @@ void free_matches(struct search_context *context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find the first occurrence of 'match' in 'string' and return its index
|
||||
*
|
||||
* \param string the string to be searched (unterminated)
|
||||
* \param s_len length of the string to be searched
|
||||
* \param pattern the pattern for which we are searching (unterminated)
|
||||
* \param p_len length of pattern
|
||||
* \param case_sens true iff case sensitive match required
|
||||
* \param m_len accepts length of match in bytes
|
||||
* \return pointer to first match, NULL if none
|
||||
*/
|
||||
|
||||
static const char *find_pattern(const char *string, int s_len,
|
||||
const char *pattern, int p_len, bool case_sens,
|
||||
unsigned int *m_len)
|
||||
{
|
||||
struct { const char *ss, *s, *p; bool first; } context[16];
|
||||
const char *ep = pattern + p_len;
|
||||
const char *es = string + s_len;
|
||||
const char *p = pattern - 1; /* a virtual '*' before the pattern */
|
||||
const char *ss = string;
|
||||
const char *s = string;
|
||||
bool first = true;
|
||||
int top = 0;
|
||||
|
||||
while (p < ep) {
|
||||
bool matches;
|
||||
if (p < pattern || *p == '*') {
|
||||
char ch;
|
||||
|
||||
/* skip any further asterisks; one is the same as many
|
||||
*/
|
||||
do p++; while (p < ep && *p == '*');
|
||||
|
||||
/* if we're at the end of the pattern, yes, it matches
|
||||
*/
|
||||
if (p >= ep) break;
|
||||
|
||||
/* anything matches a # so continue matching from
|
||||
here, and stack a context that will try to match
|
||||
the wildcard against the next character */
|
||||
|
||||
ch = *p;
|
||||
if (ch != '#') {
|
||||
/* scan forwards until we find a match for
|
||||
this char */
|
||||
if (!case_sens) ch = toupper(ch);
|
||||
while (s < es) {
|
||||
if (case_sens) {
|
||||
if (*s == ch) break;
|
||||
} else if (toupper(*s) == ch)
|
||||
break;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
if (s < es) {
|
||||
/* remember where we are in case the match
|
||||
fails; we may then resume */
|
||||
if (top < (int)NOF_ELEMENTS(context)) {
|
||||
context[top].ss = ss;
|
||||
context[top].s = s + 1;
|
||||
context[top].p = p - 1;
|
||||
/* ptr to last asterisk */
|
||||
context[top].first = first;
|
||||
top++;
|
||||
}
|
||||
|
||||
if (first) {
|
||||
ss = s;
|
||||
/* remember first non-'*' char */
|
||||
first = false;
|
||||
}
|
||||
|
||||
matches = true;
|
||||
}
|
||||
else
|
||||
matches = false;
|
||||
}
|
||||
else if (s < es) {
|
||||
char ch = *p;
|
||||
if (ch == '#')
|
||||
matches = true;
|
||||
else {
|
||||
if (case_sens)
|
||||
matches = (*s == ch);
|
||||
else
|
||||
matches = (toupper(*s) == toupper(ch));
|
||||
}
|
||||
if (matches && first) {
|
||||
ss = s; /* remember first non-'*' char */
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
matches = false;
|
||||
|
||||
if (matches) {
|
||||
p++; s++;
|
||||
}
|
||||
else {
|
||||
/* doesn't match, resume with stacked context if we have one */
|
||||
if (--top < 0) return NULL; /* no match, give up */
|
||||
|
||||
ss = context[top].ss;
|
||||
s = context[top].s;
|
||||
p = context[top].p;
|
||||
first = context[top].first;
|
||||
}
|
||||
}
|
||||
|
||||
/* end of pattern reached */
|
||||
*m_len = max(s - ss, 1);
|
||||
return ss;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a new entry to the list of matches
|
||||
*
|
||||
* \param start_idx offset of match start within textual representation
|
||||
* \param end_idx offset of match end
|
||||
* \return pointer to added entry, NULL iff failed
|
||||
*/
|
||||
|
||||
static struct list_entry *add_entry(unsigned start_idx, unsigned end_idx,
|
||||
struct search_context *context)
|
||||
{
|
||||
struct list_entry *entry;
|
||||
|
||||
/* found string in box => add to list */
|
||||
entry = calloc(1, sizeof(*entry));
|
||||
if (!entry) {
|
||||
warn_user("NoMemory", 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entry->start_idx = start_idx;
|
||||
entry->end_idx = end_idx;
|
||||
entry->sel = NULL;
|
||||
|
||||
entry->next = 0;
|
||||
entry->prev = context->found->prev;
|
||||
if (context->found->prev == NULL)
|
||||
context->found->next = entry;
|
||||
else
|
||||
context->found->prev->next = entry;
|
||||
context->found->prev = entry;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds all occurrences of a given string in the html box tree
|
||||
*
|
||||
* \param pattern the string pattern to search for
|
||||
* \param p_len pattern length
|
||||
* \param cur pointer to the current box
|
||||
* \param case_sens whether to perform a case sensitive search
|
||||
* \return true on success, false on memory allocation failure
|
||||
*/
|
||||
static bool find_occurrences_html(const char *pattern, int p_len,
|
||||
struct box *cur, bool case_sens,
|
||||
struct search_context *context)
|
||||
{
|
||||
struct box *a;
|
||||
|
||||
/* ignore this box, if there's no visible text */
|
||||
if (!cur->object && cur->text) {
|
||||
const char *text = cur->text;
|
||||
unsigned length = cur->length;
|
||||
|
||||
while (length > 0) {
|
||||
struct list_entry *entry;
|
||||
unsigned match_length;
|
||||
unsigned match_offset;
|
||||
const char *new_text;
|
||||
const char *pos = find_pattern(text, length,
|
||||
pattern, p_len, case_sens,
|
||||
&match_length);
|
||||
if (!pos) break;
|
||||
|
||||
/* found string in box => add to list */
|
||||
match_offset = pos - cur->text;
|
||||
|
||||
entry = add_entry(cur->byte_offset + match_offset,
|
||||
cur->byte_offset +
|
||||
match_offset +
|
||||
match_length, context);
|
||||
if (!entry)
|
||||
return false;
|
||||
|
||||
entry->start_box = cur;
|
||||
entry->end_box = cur;
|
||||
|
||||
new_text = pos + match_length;
|
||||
length -= (new_text - text);
|
||||
text = new_text;
|
||||
}
|
||||
}
|
||||
|
||||
/* and recurse */
|
||||
for (a = cur->children; a; a = a->next) {
|
||||
if (!find_occurrences_html(pattern, p_len, a, case_sens,
|
||||
context))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds all occurrences of a given string in a textplain content
|
||||
*
|
||||
* \param pattern the string pattern to search for
|
||||
* \param p_len pattern length
|
||||
* \param c the content to be searched
|
||||
* \param case_sens wheteher to perform a case sensitive search
|
||||
* \return true on success, false on memory allocation failure
|
||||
*/
|
||||
|
||||
static bool find_occurrences_text(const char *pattern, int p_len,
|
||||
struct content *c, bool case_sens,
|
||||
struct search_context *context)
|
||||
{
|
||||
int nlines = textplain_line_count(c);
|
||||
int line;
|
||||
|
||||
for(line = 0; line < nlines; line++) {
|
||||
size_t offset, length;
|
||||
const char *text = textplain_get_line(c, line,
|
||||
&offset, &length);
|
||||
if (text) {
|
||||
while (length > 0) {
|
||||
struct list_entry *entry;
|
||||
unsigned match_length;
|
||||
size_t start_idx;
|
||||
const char *new_text;
|
||||
const char *pos = find_pattern(text, length,
|
||||
pattern, p_len, case_sens,
|
||||
&match_length);
|
||||
if (!pos) break;
|
||||
|
||||
/* found string in line => add to list */
|
||||
start_idx = offset + (pos - text);
|
||||
entry = add_entry(start_idx, start_idx +
|
||||
match_length, context);
|
||||
if (!entry)
|
||||
return false;
|
||||
|
||||
new_text = pos + match_length;
|
||||
offset += (new_text - text);
|
||||
length -= (new_text - text);
|
||||
text = new_text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Search for a string in the box tree
|
||||
*
|
||||
* \param string the string to search for
|
||||
* \param string_len length of search string
|
||||
*/
|
||||
void search_text(const char *string, int string_len,
|
||||
static void search_text(const char *string, int string_len,
|
||||
struct search_context *context, search_flags_t flags)
|
||||
{
|
||||
struct rect bounds;
|
||||
@ -383,229 +587,53 @@ void search_text(const char *string, int string_len,
|
||||
content_broadcast(context->c, CONTENT_MSG_SCROLL, msg_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first occurrence of 'match' in 'string' and return its index
|
||||
*
|
||||
* \param string the string to be searched (unterminated)
|
||||
* \param s_len length of the string to be searched
|
||||
* \param pattern the pattern for which we are searching (unterminated)
|
||||
* \param p_len length of pattern
|
||||
* \param case_sens true iff case sensitive match required
|
||||
* \param m_len accepts length of match in bytes
|
||||
* \return pointer to first match, NULL if none
|
||||
*/
|
||||
|
||||
const char *find_pattern(const char *string, int s_len, const char *pattern,
|
||||
int p_len, bool case_sens, unsigned int *m_len)
|
||||
{
|
||||
struct { const char *ss, *s, *p; bool first; } context[16];
|
||||
const char *ep = pattern + p_len;
|
||||
const char *es = string + s_len;
|
||||
const char *p = pattern - 1; /* a virtual '*' before the pattern */
|
||||
const char *ss = string;
|
||||
const char *s = string;
|
||||
bool first = true;
|
||||
int top = 0;
|
||||
|
||||
while (p < ep) {
|
||||
bool matches;
|
||||
if (p < pattern || *p == '*') {
|
||||
char ch;
|
||||
|
||||
/* skip any further asterisks; one is the same as many
|
||||
*/
|
||||
do p++; while (p < ep && *p == '*');
|
||||
|
||||
/* if we're at the end of the pattern, yes, it matches
|
||||
*/
|
||||
if (p >= ep) break;
|
||||
|
||||
/* anything matches a # so continue matching from
|
||||
here, and stack a context that will try to match
|
||||
the wildcard against the next character */
|
||||
|
||||
ch = *p;
|
||||
if (ch != '#') {
|
||||
/* scan forwards until we find a match for
|
||||
this char */
|
||||
if (!case_sens) ch = toupper(ch);
|
||||
while (s < es) {
|
||||
if (case_sens) {
|
||||
if (*s == ch) break;
|
||||
} else if (toupper(*s) == ch)
|
||||
break;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
if (s < es) {
|
||||
/* remember where we are in case the match
|
||||
fails; we may then resume */
|
||||
if (top < (int)NOF_ELEMENTS(context)) {
|
||||
context[top].ss = ss;
|
||||
context[top].s = s + 1;
|
||||
context[top].p = p - 1;
|
||||
/* ptr to last asterisk */
|
||||
context[top].first = first;
|
||||
top++;
|
||||
}
|
||||
|
||||
if (first) {
|
||||
ss = s;
|
||||
/* remember first non-'*' char */
|
||||
first = false;
|
||||
}
|
||||
|
||||
matches = true;
|
||||
}
|
||||
else
|
||||
matches = false;
|
||||
}
|
||||
else if (s < es) {
|
||||
char ch = *p;
|
||||
if (ch == '#')
|
||||
matches = true;
|
||||
else {
|
||||
if (case_sens)
|
||||
matches = (*s == ch);
|
||||
else
|
||||
matches = (toupper(*s) == toupper(ch));
|
||||
}
|
||||
if (matches && first) {
|
||||
ss = s; /* remember first non-'*' char */
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
matches = false;
|
||||
|
||||
if (matches) {
|
||||
p++; s++;
|
||||
}
|
||||
else {
|
||||
/* doesn't match, resume with stacked context if we have one */
|
||||
if (--top < 0) return NULL; /* no match, give up */
|
||||
|
||||
ss = context[top].ss;
|
||||
s = context[top].s;
|
||||
p = context[top].p;
|
||||
first = context[top].first;
|
||||
}
|
||||
}
|
||||
|
||||
/* end of pattern reached */
|
||||
*m_len = max(s - ss, 1);
|
||||
return ss;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all occurrences of a given string in the html box tree
|
||||
* Begins/continues the search process
|
||||
* Note that this may be called many times for a single search.
|
||||
*
|
||||
* \param pattern the string pattern to search for
|
||||
* \param p_len pattern length
|
||||
* \param cur pointer to the current box
|
||||
* \param case_sens whether to perform a case sensitive search
|
||||
* \return true on success, false on memory allocation failure
|
||||
*/
|
||||
bool find_occurrences_html(const char *pattern, int p_len, struct box *cur,
|
||||
bool case_sens, struct search_context *context)
|
||||
{
|
||||
struct box *a;
|
||||
|
||||
/* ignore this box, if there's no visible text */
|
||||
if (!cur->object && cur->text) {
|
||||
const char *text = cur->text;
|
||||
unsigned length = cur->length;
|
||||
|
||||
while (length > 0) {
|
||||
struct list_entry *entry;
|
||||
unsigned match_length;
|
||||
unsigned match_offset;
|
||||
const char *new_text;
|
||||
const char *pos = find_pattern(text, length,
|
||||
pattern, p_len, case_sens,
|
||||
&match_length);
|
||||
if (!pos) break;
|
||||
|
||||
/* found string in box => add to list */
|
||||
match_offset = pos - cur->text;
|
||||
|
||||
entry = add_entry(cur->byte_offset + match_offset,
|
||||
cur->byte_offset +
|
||||
match_offset +
|
||||
match_length, context);
|
||||
if (!entry)
|
||||
return false;
|
||||
|
||||
entry->start_box = cur;
|
||||
entry->end_box = cur;
|
||||
|
||||
new_text = pos + match_length;
|
||||
length -= (new_text - text);
|
||||
text = new_text;
|
||||
}
|
||||
}
|
||||
|
||||
/* and recurse */
|
||||
for (a = cur->children; a; a = a->next) {
|
||||
if (!find_occurrences_html(pattern, p_len, a, case_sens,
|
||||
context))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all occurrences of a given string in a textplain content
|
||||
*
|
||||
* \param pattern the string pattern to search for
|
||||
* \param p_len pattern length
|
||||
* \param c the content to be searched
|
||||
* \param case_sens wheteher to perform a case sensitive search
|
||||
* \return true on success, false on memory allocation failure
|
||||
* \param bw the browser_window to search in
|
||||
* \param flags the flags forward/back etc
|
||||
* \param string the string to match
|
||||
*/
|
||||
|
||||
bool find_occurrences_text(const char *pattern, int p_len,
|
||||
struct content *c, bool case_sens,
|
||||
struct search_context *context)
|
||||
void search_step(struct search_context *context, search_flags_t flags,
|
||||
const char *string)
|
||||
{
|
||||
int nlines = textplain_line_count(c);
|
||||
int line;
|
||||
int string_len;
|
||||
int i = 0;
|
||||
|
||||
for(line = 0; line < nlines; line++) {
|
||||
size_t offset, length;
|
||||
const char *text = textplain_get_line(c, line,
|
||||
&offset, &length);
|
||||
if (text) {
|
||||
while (length > 0) {
|
||||
struct list_entry *entry;
|
||||
unsigned match_length;
|
||||
size_t start_idx;
|
||||
const char *new_text;
|
||||
const char *pos = find_pattern(text, length,
|
||||
pattern, p_len, case_sens,
|
||||
&match_length);
|
||||
if (!pos) break;
|
||||
|
||||
/* found string in line => add to list */
|
||||
start_idx = offset + (pos - text);
|
||||
entry = add_entry(start_idx, start_idx +
|
||||
match_length, context);
|
||||
if (!entry)
|
||||
return false;
|
||||
|
||||
new_text = pos + match_length;
|
||||
offset += (new_text - text);
|
||||
length -= (new_text - text);
|
||||
text = new_text;
|
||||
}
|
||||
}
|
||||
if ((context == NULL) || (context->callbacks == NULL)) {
|
||||
warn_user("SearchError", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (context->callbacks->add_recent != NULL)
|
||||
context->callbacks->add_recent(string, context->p);
|
||||
|
||||
string_len = strlen(string);
|
||||
for(i = 0; i < string_len; i++)
|
||||
if (string[i] != '#' && string[i] != '*') break;
|
||||
if (i >= string_len) {
|
||||
union content_msg_data msg_data;
|
||||
free_matches(context);
|
||||
if (context->callbacks->status != NULL)
|
||||
context->callbacks->status(true, context->p);
|
||||
if (context->callbacks->back_state != NULL)
|
||||
context->callbacks->back_state(false, context->p);
|
||||
if (context->callbacks->forward_state != NULL)
|
||||
context->callbacks->forward_state(false, context->p);
|
||||
|
||||
msg_data.scroll.area = false;
|
||||
msg_data.scroll.x0 = 0;
|
||||
msg_data.scroll.y0 = 0;
|
||||
content_broadcast(context->c, CONTENT_MSG_SCROLL, msg_data);
|
||||
return;
|
||||
}
|
||||
search_text(string, string_len, context, flags);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether any portion of the given text box should be
|
||||
* selected because it matches the current search string.
|
||||
@ -636,6 +664,7 @@ bool search_term_highlighted(struct content *c,
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specifies whether all matches or just the current match should
|
||||
* be highlighted in the search text.
|
||||
@ -678,40 +707,6 @@ void search_show_all(bool all, struct search_context *context)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new entry to the list of matches
|
||||
*
|
||||
* \param start_idx offset of match start within textual representation
|
||||
* \param end_idx offset of match end
|
||||
* \return pointer to added entry, NULL iff failed
|
||||
*/
|
||||
|
||||
struct list_entry *add_entry(unsigned start_idx, unsigned end_idx,
|
||||
struct search_context *context)
|
||||
{
|
||||
struct list_entry *entry;
|
||||
|
||||
/* found string in box => add to list */
|
||||
entry = calloc(1, sizeof(*entry));
|
||||
if (!entry) {
|
||||
warn_user("NoMemory", 0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
entry->start_idx = start_idx;
|
||||
entry->end_idx = end_idx;
|
||||
entry->sel = NULL;
|
||||
|
||||
entry->next = 0;
|
||||
entry->prev = context->found->prev;
|
||||
if (context->found->prev == NULL)
|
||||
context->found->next = entry;
|
||||
else
|
||||
context->found->prev->next = entry;
|
||||
context->found->prev = entry;
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends the search process, invalidating all state
|
||||
@ -742,4 +737,3 @@ void search_destroy_context(struct search_context *context)
|
||||
free_matches(context);
|
||||
free(context);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user