elm_code: add text search functions.

Update the search and replace UI to work with the new elm_ode UI.
This commit is contained in:
Andy Williams 2015-03-22 22:36:04 +00:00
parent ef4e5ee686
commit e2718aac97
3 changed files with 83 additions and 0 deletions

View File

@ -42,6 +42,38 @@ elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int leng
elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line);
}
EAPI int
elm_code_line_text_strpos(Elm_Code_Line *line, const char *search, int offset)
{
unsigned int length, searchlen, c;
const char *content;
char *ptr;
searchlen = strlen(search);
content = elm_code_line_text_get(line, &length);
ptr = (char *) content;
if (searchlen > length)
return ELM_CODE_TEXT_NOT_FOUND;
ptr += offset;
for (c = offset; c < length - strlen(search); c++)
{
if (!strncmp(ptr, search, searchlen))
return c;
ptr++;
}
return ELM_CODE_TEXT_NOT_FOUND;
}
EAPI Eina_Bool
elm_code_line_text_contains(Elm_Code_Line *line, const char *search)
{
return elm_code_line_text_strpos(line, search, 0) != ELM_CODE_TEXT_NOT_FOUND;
}
static void
_elm_code_line_tokens_move_right(Elm_Code_Line *line, int position, int move)
{

View File

@ -1,6 +1,8 @@
#ifndef ELM_CODE_TEXT_H_
# define ELM_CODE_TEXT_H_
#define ELM_CODE_TEXT_NOT_FOUND -1
#ifdef __cplusplus
extern "C" {
#endif
@ -24,6 +26,10 @@ EAPI const char *elm_code_line_text_get(Elm_Code_Line *line, unsigned int *lengt
EAPI void elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length);
EAPI int elm_code_line_text_strpos(Elm_Code_Line *line, const char *search, int offset);
EAPI Eina_Bool elm_code_line_text_contains(Elm_Code_Line *line, const char *search);
EAPI void elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, const char *string, int length);
EAPI void elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length);

View File

@ -37,8 +37,53 @@ START_TEST (elm_code_text_insert_test)
}
END_TEST
START_TEST (elm_code_text_contains_test)
{
Elm_Code *code;
Elm_Code_File *file;
Elm_Code_Line *line;
code = elm_code_create();
file = elm_code_file_new(code);
elm_code_file_line_append(file, "a test string...", 17, NULL);
line = elm_code_file_line_get(file, 1);
ck_assert_int_eq(EINA_TRUE, elm_code_line_text_contains(line, "test"));
ck_assert_int_eq(EINA_FALSE, elm_code_line_text_contains(line, "text"));
ck_assert_int_eq(EINA_TRUE, elm_code_line_text_contains(line, "a t"));
ck_assert_int_eq(EINA_TRUE, elm_code_line_text_contains(line, "..."));
}
END_TEST
START_TEST (elm_code_text_strpos_test)
{
Elm_Code *code;
Elm_Code_File *file;
Elm_Code_Line *line;
code = elm_code_create();
file = elm_code_file_new(code);
elm_code_file_line_append(file, "a test string...", 17, NULL);
line = elm_code_file_line_get(file, 1);
ck_assert_int_eq(2, elm_code_line_text_strpos(line, "test", 0));
ck_assert_int_eq(2, elm_code_line_text_strpos(line, "test", 1));
ck_assert_int_eq(2, elm_code_line_text_strpos(line, "test", 2));
ck_assert_int_eq(ELM_CODE_TEXT_NOT_FOUND, elm_code_line_text_strpos(line, "test", 5));
ck_assert_int_eq(ELM_CODE_TEXT_NOT_FOUND, elm_code_line_text_strpos(line, "text", 0));
ck_assert_int_eq(0, elm_code_line_text_strpos(line, "a t", 0));
ck_assert_int_eq(13, elm_code_line_text_strpos(line, "...", 0));
}
END_TEST
void elm_code_test_text(TCase *tc)
{
tcase_add_test(tc, elm_code_text_get_test);
tcase_add_test(tc, elm_code_text_insert_test);
tcase_add_test(tc, elm_code_text_contains_test);
tcase_add_test(tc, elm_code_text_strpos_test);
}