diff options
author | Andy Williams <andy@andywilliams.me> | 2015-08-29 12:05:56 +0100 |
---|---|---|
committer | Andy Williams <andy@andywilliams.me> | 2015-08-29 12:05:56 +0100 |
commit | 1e5e74d94707158d686c8718a8205049f4b6ac30 (patch) | |
tree | 0d9ead9dcd9d7d282286b77bb6c249a36ee9f91e | |
parent | d5804b08374268c108c32c5775bf1abed56894e2 (diff) | |
download | efl-1e5e74d94707158d686c8718a8205049f4b6ac30.tar.gz |
[elm_code] trim all trailing whitespace on save
Rather than just blank lines remove all trailing whitespace from lines
-rw-r--r-- | legacy/elm_code/src/lib/elm_code_file.c | 7 | ||||
-rw-r--r-- | legacy/elm_code/src/lib/elm_code_text.c | 53 | ||||
-rw-r--r-- | legacy/elm_code/src/lib/elm_code_text.h | 4 | ||||
-rw-r--r-- | legacy/elm_code/src/tests/elm_code_test_text.c | 19 |
4 files changed, 74 insertions, 9 deletions
diff --git a/legacy/elm_code/src/lib/elm_code_file.c b/legacy/elm_code/src/lib/elm_code_file.c index 8832fe0ad7..387e249d47 100644 --- a/legacy/elm_code/src/lib/elm_code_file.c +++ b/legacy/elm_code/src/lib/elm_code_file.c @@ -178,12 +178,9 @@ EAPI void elm_code_file_save(Elm_Code_File *file) EINA_LIST_FOREACH(file->lines, item, line_item) { + elm_code_line_text_trailing_whitespace_strip(line_item); content = elm_code_line_text_get(line_item, &length); - if (elm_code_text_is_whitespace(content, length)) - { - length = 0; - elm_code_line_text_set(line_item, "", 0); - } + fwrite(content, sizeof(char), length, out); fwrite(crchars, sizeof(char), crlength, out); } diff --git a/legacy/elm_code/src/lib/elm_code_text.c b/legacy/elm_code/src/lib/elm_code_text.c index 83216c6cba..abb5a6968d 100644 --- a/legacy/elm_code/src/lib/elm_code_text.c +++ b/legacy/elm_code/src/lib/elm_code_text.c @@ -24,19 +24,22 @@ EAPI void elm_code_line_text_set(Elm_Code_Line *line, const char *chars, unsigned int length) { Elm_Code_File *file; - char *newtext; + char *newtext, *oldtext = NULL; if (!line) return; if (line->modified) - free(line->modified); + oldtext = line->modified; newtext = malloc(sizeof(char) * length); strncpy(newtext, chars, length); line->modified = newtext; line->length = length; + if (oldtext) + free(oldtext); + file = line->file; if (file->parent) { @@ -207,6 +210,20 @@ elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length elm_code_callback_fire(file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line); } +EAPI void elm_code_line_text_trailing_whitespace_strip(Elm_Code_Line *line) +{ + unsigned int length, trailing; + const char *content; + + content = elm_code_line_text_get(line, &length); + trailing = elm_code_text_trailing_whitespace_length(content, length); + if (trailing == 0) + return; + + length -= trailing;; + elm_code_line_text_set(line, content, length); +} + /* generic text functions */ EAPI int @@ -238,6 +255,12 @@ elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen) return crpos; } +static Eina_Bool +_elm_code_text_char_is_whitespace(char c) +{ + return c == ' ' || c == '\t'; +} + EAPI unsigned int elm_code_text_leading_whitespace_length(const char *text, unsigned int length) { @@ -246,7 +269,7 @@ elm_code_text_leading_whitespace_length(const char *text, unsigned int length) while (count < length) { - if (!(*ptr == ' ' || *ptr == '\t')) + if (!_elm_code_text_char_is_whitespace(*ptr)) break; count++; @@ -257,11 +280,33 @@ elm_code_text_leading_whitespace_length(const char *text, unsigned int length) } EAPI unsigned int +elm_code_text_trailing_whitespace_length(const char *text, unsigned int length) +{ + unsigned int count = 0; + char *ptr; + + if (length == 0) + return 0; + + ptr = (char *)text + length - 1; + while (count < length) + { + if (!_elm_code_text_char_is_whitespace(*ptr)) + break; + + count++; + ptr--; + } + + return count; +} + +EAPI unsigned int elm_code_text_is_whitespace(const char *text, unsigned int length) { unsigned int leading; - leading = elm_code_text_leading_whitespace_length(text, length); + leading = elm_code_text_trailing_whitespace_length(text, length); return leading == length; } diff --git a/legacy/elm_code/src/lib/elm_code_text.h b/legacy/elm_code/src/lib/elm_code_text.h index 66bfbffb3f..02d2cc4ad5 100644 --- a/legacy/elm_code/src/lib/elm_code_text.h +++ b/legacy/elm_code/src/lib/elm_code_text.h @@ -36,6 +36,8 @@ EAPI void elm_code_line_text_insert(Elm_Code_Line *line, unsigned int position, EAPI void elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length); +EAPI void elm_code_line_text_trailing_whitespace_strip(Elm_Code_Line *line); + /** * @} * @@ -54,6 +56,8 @@ EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, short EAPI unsigned int elm_code_text_leading_whitespace_length(const char *text, unsigned int length); +EAPI unsigned int elm_code_text_trailing_whitespace_length(const char *text, unsigned int length); + EAPI unsigned int elm_code_text_is_whitespace(const char *text, unsigned int length); /** diff --git a/legacy/elm_code/src/tests/elm_code_test_text.c b/legacy/elm_code/src/tests/elm_code_test_text.c index 9644ba203b..26e1aa18de 100644 --- a/legacy/elm_code/src/tests/elm_code_test_text.c +++ b/legacy/elm_code/src/tests/elm_code_test_text.c @@ -112,6 +112,24 @@ START_TEST (elm_code_text_leading_whitespace_test) } END_TEST +START_TEST (elm_code_text_trailing_whitespace_test) +{ + const char *text; + + text = "testing"; + ck_assert_int_eq(0, elm_code_text_trailing_whitespace_length(text, strlen(text))); + + text = "spaces "; + ck_assert_int_eq(2, elm_code_text_trailing_whitespace_length(text, strlen(text))); + + text = "tabs\t\t"; + ck_assert_int_eq(2, elm_code_text_trailing_whitespace_length(text, strlen(text))); + + text = "mix \t "; + ck_assert_int_eq(3, elm_code_text_trailing_whitespace_length(text, strlen(text))); +} +END_TEST + START_TEST (elm_code_text_is_whitespace_test) { const char *text; @@ -135,5 +153,6 @@ void elm_code_test_text(TCase *tc) tcase_add_test(tc, elm_code_text_strpos_test); tcase_add_test(tc, elm_code_text_newline_position_test); tcase_add_test(tc, elm_code_text_leading_whitespace_test); + tcase_add_test(tc, elm_code_text_trailing_whitespace_test); tcase_add_test(tc, elm_code_text_is_whitespace_test); } |