1 /* xbt_str.c - various helping functions to deal with strings */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
12 #include "xbt/str.h" /* headers of these functions */
13 #include "xbt/strbuff.h"
14 #include "xbt/matrix.h" /* for the diff */
16 /** @brief Strip whitespace (or other characters) from the end of a string.
18 * Strips the whitespaces from the end of s.
19 * By default (when char_list=NULL), these characters get stripped:
21 * - " " (ASCII 32 (0x20)) space.
22 * - "\t" (ASCII 9 (0x09)) tab.
23 * - "\n" (ASCII 10 (0x0A)) line feed.
24 * - "\r" (ASCII 13 (0x0D)) carriage return.
25 * - "\0" (ASCII 0 (0x00)) NULL.
26 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
28 * @param s The string to strip. Modified in place.
29 * @param char_list A string which contains the characters you want to strip.
32 void xbt_str_rtrim(char *s, const char *char_list)
35 const char *__char_list = " \t\n\r\x0B";
36 char white_char[256] = { 1, 0 };
42 while (*__char_list) {
43 white_char[(unsigned char) *__char_list++] = 1;
47 white_char[(unsigned char) *char_list++] = 1;
54 while ((cur >= s) && white_char[(unsigned char) *cur])
60 /** @brief Strip whitespace (or other characters) from the beginning of a string.
62 * Strips the whitespaces from the begining of s.
63 * By default (when char_list=NULL), these characters get stripped:
65 * - " " (ASCII 32 (0x20)) space.
66 * - "\t" (ASCII 9 (0x09)) tab.
67 * - "\n" (ASCII 10 (0x0A)) line feed.
68 * - "\r" (ASCII 13 (0x0D)) carriage return.
69 * - "\0" (ASCII 0 (0x00)) NULL.
70 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
72 * @param s The string to strip. Modified in place.
73 * @param char_list A string which contains the characters you want to strip.
76 void xbt_str_ltrim(char *s, const char *char_list)
79 const char *__char_list = " \t\n\r\x0B";
80 char white_char[256] = { 1, 0 };
86 while (*__char_list) {
87 white_char[(unsigned char) *__char_list++] = 1;
91 white_char[(unsigned char) *char_list++] = 1;
95 while (*cur && white_char[(unsigned char) *cur])
98 memmove(s, cur, strlen(cur) + 1);
101 /** @brief Strip whitespace (or other characters) from the end and the begining of a string.
103 * Strips the whitespaces from both the beginning and the end of s.
104 * By default (when char_list=NULL), these characters get stripped:
106 * - " " (ASCII 32 (0x20)) space.
107 * - "\t" (ASCII 9 (0x09)) tab.
108 * - "\n" (ASCII 10 (0x0A)) line feed.
109 * - "\r" (ASCII 13 (0x0D)) carriage return.
110 * - "\0" (ASCII 0 (0x00)) NULL.
111 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
113 * @param s The string to strip.
114 * @param char_list A string which contains the characters you want to strip.
117 void xbt_str_trim(char *s, const char *char_list)
123 xbt_str_rtrim(s, char_list);
124 xbt_str_ltrim(s, char_list);
127 /** @brief Replace double whitespaces (but no other characters) from the string.
129 * The function modifies the string so that each time that several spaces appear,
130 * they are replaced by a single space. It will only do so for spaces (ASCII 32, 0x20).
132 * @param s The string to strip. Modified in place.
135 void xbt_str_strip_spaces(char *s)
171 /** @brief Substitutes a char for another in a string
173 * @param str the string to modify
174 * @param from char to search
175 * @param to char to put instead
176 * @param occurence number of changes to do (=0 means all)
178 void xbt_str_subst(char *str, char from, char to, int occurence)
192 /** @brief Replaces a set of variables by their values
194 * @param str The input of the replacement process
195 * @param patterns The changes to apply
196 * @return The string modified
198 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
200 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
202 * You can provide a default value to use if the variable is not set in the dict by using
203 * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they
204 * shouldn't to respect the shell standard (:= form should set the value in the dict,
205 * but does not) (BUG).
208 char *xbt_str_varsubst(const char *str, xbt_dict_t patterns)
210 xbt_strbuff_t buff = xbt_strbuff_new_from(str);
212 xbt_strbuff_varsubst(buff, patterns);
214 xbt_strbuff_free_container(buff);
219 /** @brief Splits a string into a dynar of strings
221 * @param s: the string to split
222 * @param sep: a string of all chars to consider as separator.
224 * By default (with sep=NULL), these characters are used as separator:
226 * - " " (ASCII 32 (0x20)) space.
227 * - "\t" (ASCII 9 (0x09)) tab.
228 * - "\n" (ASCII 10 (0x0A)) line feed.
229 * - "\r" (ASCII 13 (0x0D)) carriage return.
230 * - "\0" (ASCII 0 (0x00)) NULL.
231 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
234 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
236 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
239 const char *sep_dflt = " \t\n\r\x0B";
240 char is_sep[256] = { 1, 0 };
242 /* check what are the separators */
243 memset(is_sep, 0, sizeof(is_sep));
246 is_sep[(unsigned char) *sep_dflt++] = 1;
249 is_sep[(unsigned char) *sep++] = 1;
251 is_sep[0] = 1; /* End of string is also separator */
262 while (!is_sep[(unsigned char) *q]) {
268 topush = xbt_malloc(q - p + 1);
269 memcpy(topush, p, q - p);
270 topush[q - p] = '\0';
271 xbt_dynar_push(res, &topush);
279 * \brief This functions splits a string after using another string as separator
280 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
281 * \return An array of dynars containing the string tokens
283 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
285 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
294 if (sep[0] == '\0') {
296 xbt_dynar_push(res, &s);
303 //get the start of the first occurence of the substring
305 //if substring was not found add the entire string
308 to_push = malloc(v + 1);
309 memcpy(to_push, p, v);
311 xbt_dynar_push(res, &to_push);
315 to_push = malloc(q - p + 1);
316 memcpy(to_push, p, q - p);
317 //add string terminator
318 to_push[q - p] = '\0';
319 xbt_dynar_push(res, &to_push);
326 /** @brief Just like @ref xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
328 * The string passed as argument must be writable (not const)
329 * The elements of the dynar are just parts of the string passed as argument.
331 * To free the structure constructed by this function, free the first element and free the dynar:
333 * free(xbt_dynar_get_ptr(dynar,0));
334 * xbt_dynar_free(&dynar);
336 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
337 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), NULL);
338 char *beg, *end; /* pointers around the parsed chunk */
339 int in_simple_quote = 0, in_double_quote = 0;
341 int ctn = 0; /* Got something in this block */
348 /* do not trim leading spaces: caller responsability to clean his cruft */
357 /* Protected char; move it closer */
358 memmove(end, end + 1, strlen(end));
360 THROWF(arg_error, 0, "String ends with \\");
361 end++; /* Pass the protected char */
366 if (!in_double_quote) {
367 in_simple_quote = !in_simple_quote;
368 memmove(end, end + 1, strlen(end));
370 /* simple quote protected by double ones */
376 if (!in_simple_quote) {
377 in_double_quote = !in_double_quote;
378 memmove(end, end + 1, strlen(end));
380 /* double quote protected by simple ones */
389 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
391 "End of string found while searching for %c in %s",
392 (in_simple_quote ? '\'' : '"'), s);
394 if (in_simple_quote || in_double_quote) {
402 /* Found a separator. Push the string if contains something */
403 xbt_dynar_push(res, &beg);
411 /* trim within the string, manually to speed things up */
426 /** @brief Splits a string into a dynar of strings, taking quotes into account
428 * It basically does the same argument separation than the shell, where white
429 * spaces can be escaped and where arguments are never split within a
431 * Several subsequent spaces are ignored (unless within quotes, of course).
432 * You may want to trim the input string, if you want to avoid empty entries
436 xbt_dynar_t xbt_str_split_quoted(const char *s)
438 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
440 char *str_to_free; /* we have to copy the string before, to handle backslashes */
446 str_to_free = xbt_strdup(s);
448 parsed = xbt_str_split_quoted_in_place(str_to_free);
449 xbt_dynar_foreach(parsed,cursor,p) {
450 char *q=xbt_strdup(p);
451 xbt_dynar_push(res,&q);
454 xbt_dynar_shrink(res, 0);
455 xbt_dynar_free(&parsed);
459 /** @brief Join a set of strings as a single string */
460 char *xbt_str_join(xbt_dynar_t dyn, const char *sep)
462 int len = 1, dyn_len = xbt_dynar_length(dyn);
468 return xbt_strdup("");
470 /* compute the length */
471 xbt_dynar_foreach(dyn, cpt, cursor) {
472 len += strlen(cursor);
474 len += strlen(sep) * dyn_len;
476 res = xbt_malloc(len);
478 xbt_dynar_foreach(dyn, cpt, cursor) {
479 if ((int) cpt < dyn_len - 1)
480 p += sprintf(p, "%s%s", cursor, sep);
482 p += sprintf(p, "%s", cursor);
486 /** @brief Join a set of strings as a single string
488 * The parameter must be a NULL-terminated array of chars,
489 * just like xbt_dynar_to_array() produces
491 char *xbt_str_join_array(const char *const *strs, const char *sep)
494 int amount_strings=0;
498 if ((!strs) || (!strs[0]))
499 return xbt_strdup("");
501 /* compute the length before malloc */
502 for (i=0;strs[i];i++) {
503 len += strlen(strs[i]);
506 len += strlen(sep) * amount_strings;
509 q = res = xbt_malloc(len);
510 for (i=0;strs[i];i++) {
511 if (i!=0) { // not first loop
512 q += sprintf(q, "%s%s", sep, strs[i]);
514 q += sprintf(q,"%s",strs[i]);
520 #if defined(SIMGRID_NEED_GETLINE) || defined(DOXYGEN)
521 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
523 * This is a redefinition of the GNU getline function, used on platforms where it does not exists.
525 * getline() reads an entire line from stream, storing the address of the buffer
526 * containing the text into *buf. The buffer is null-terminated and includes
527 * the newline character, if one was found.
529 * If *buf is NULL, then getline() will allocate a buffer for storing the line,
530 * which should be freed by the user program. Alternatively, before calling getline(),
531 * *buf can contain a pointer to a malloc()-allocated buffer *n bytes in size. If the buffer
532 * is not large enough to hold the line, getline() resizes it with realloc(), updating *buf and *n
533 * as necessary. In either case, on a successful call, *buf and *n will be updated to
534 * reflect the buffer address and allocated size respectively.
536 long getline(char **buf, size_t * n, FILE * stream)
543 *buf = xbt_malloc(512);
548 return (ssize_t) - 1;
550 for (i = 0; (ch = fgetc(stream)) != EOF; i++) {
553 *buf = xbt_realloc(*buf, *n += 512);
557 if ((*buf)[i] == '\n') {
565 *buf = xbt_realloc(*buf, *n += 1);
572 #endif /* HAVE_GETLINE */
575 * Diff related functions
577 * Implementation of the algorithm described in "An O(NP) Sequence Comparison
578 * Algorithm", by Sun Wu, Udi Manber, Gene Myers, and Webb Miller (Information
579 * Processing Letters 35(6):317-323, 1990), with the linear-space
580 * divide-and-conquer strategy described in "An O(ND) Difference Algorithm and
581 * Its Variations", by Eugene W. Myers (Algorithmica 1:251-266, 1986).
585 int x, y; /* starting coordinates */
586 int len; /* length */
590 void diff_snake(const char *vec_a[], int a0, int len_a,
591 const char *vec_b[], int b0, int len_b,
592 struct subsequence *seqs, int *fp, int k, int limit)
596 int fp_left = fp[k - 1] + 1;
597 int fp_right = fp[k + 1];
598 if (fp_left > fp_right) {
606 if (x + y <= limit) {
611 seqs[k] = seqs[record_seq];
613 while (x < len_a && y < len_b && !strcmp(vec_a[a0 + x], vec_b[b0 + y]))
617 seqs[k].len = x - seqs[k].x;
620 /* Returns the length of a shortest edit script, and a common
621 * subsequence from the middle.
623 static int diff_middle_subsequence(const char *vec_a[], int a0, int len_a,
624 const char *vec_b[], int b0, int len_b,
625 struct subsequence *subseq,
626 struct subsequence *seqs, int *fp)
628 const int delta = len_a - len_b;
629 const int limit = (len_a + len_b) / 2;
642 for (k = kmin; k <= kmax; k++)
646 fp[kmin - p - 1] = fp[kmax + p + 1] = -1;
647 for (k = kmax + p; k > delta; k--)
648 diff_snake(vec_a, a0, len_a, vec_b, b0, len_b, seqs, fp, k, limit);
649 for (k = kmin - p; k <= delta; k++)
650 diff_snake(vec_a, a0, len_a, vec_b, b0, len_b, seqs, fp, k, limit);
651 } while (fp[delta] != len_a);
653 subseq->x = a0 + seqs[delta].x;
654 subseq->y = b0 + seqs[delta].y;
655 subseq->len = seqs[delta].len;
656 return abs(delta) + 2 * p;;
659 /* Finds a longest common subsequence.
660 * Returns its length.
662 static int diff_compute_lcs(const char *vec_a[], int a0, int len_a,
663 const char *vec_b[], int b0, int len_b,
664 xbt_dynar_t common_sequence,
665 struct subsequence *seqs, int *fp)
667 if (len_a > 0 && len_b > 0) {
668 struct subsequence subseq;
669 int ses_len = diff_middle_subsequence(vec_a, a0, len_a, vec_b, b0, len_b,
671 int lcs_len = (len_a + len_b - ses_len) / 2;
674 } else if (ses_len > 1) {
675 int lcs_len1 = subseq.len;
676 if (lcs_len1 < lcs_len)
677 lcs_len1 += diff_compute_lcs(vec_a, a0, subseq.x - a0,
678 vec_b, b0, subseq.y - b0,
679 common_sequence, seqs, fp);
681 xbt_dynar_push(common_sequence, &subseq);
682 if (lcs_len1 < lcs_len) {
683 int u = subseq.x + subseq.len;
684 int v = subseq.y + subseq.len;
685 diff_compute_lcs(vec_a, u, a0 + len_a - u, vec_b, v, b0 + len_b - v,
686 common_sequence, seqs, fp);
689 int len = MIN(len_a, len_b) - subseq.len;
690 if (subseq.x == a0 && subseq.y == b0) {
692 xbt_dynar_push(common_sequence, &subseq);
694 struct subsequence subseq0 = {a0 + len_a - len,
695 b0 + len_b - len, len};
696 xbt_dynar_push(common_sequence, &subseq0);
700 struct subsequence subseq0 = {a0, b0, len};
701 xbt_dynar_push(common_sequence, &subseq0);
704 xbt_dynar_push(common_sequence, &subseq);
713 static int diff_member(const char *s, const char *vec[], int from, int to)
715 for ( ; from < to ; from++)
716 if (!strcmp(s, vec[from]))
721 /* Extract common prefix.
723 static void diff_easy_prefix(const char *vec_a[], int *a0_p, int *len_a_p,
724 const char *vec_b[], int *b0_p, int *len_b_p,
725 xbt_dynar_t common_sequence)
729 int len_a = *len_a_p;
730 int len_b = *len_b_p;
732 while (len_a > 0 && len_b > 0) {
733 struct subsequence subseq = {a0, b0, 0};
734 while (len_a > 0 && len_b > 0 && !strcmp(vec_a[a0], vec_b[b0])) {
740 xbt_dynar_push(common_sequence, &subseq);
741 if (len_a > 0 && len_b > 0 &&
742 !diff_member(vec_a[a0], vec_b, b0 + 1, b0 + len_b)) {
755 /* Extract common suffix.
757 static void diff_easy_suffix(const char *vec_a[], int *a0_p, int *len_a_p,
758 const char *vec_b[], int *b0_p, int *len_b_p,
759 xbt_dynar_t common_suffix)
763 int len_a = *len_a_p;
764 int len_b = *len_b_p;
766 while (len_a > 0 && len_b > 0){
767 struct subsequence subseq;
769 while (len_a > 0 && len_b > 0 &&
770 !strcmp(vec_a[a0 + len_a - 1], vec_b[b0 + len_b - 1])) {
775 if (subseq.len > 0) {
776 subseq.x = a0 + len_a;
777 subseq.y = b0 + len_b;
778 xbt_dynar_push(common_suffix, &subseq);
780 if (len_a > 0 && len_b > 0 &&
781 !diff_member(vec_b[b0 + len_b - 1], vec_a, a0, a0 + len_a - 1)) {
794 /** @brief Compute the unified diff of two strings */
795 char *xbt_str_diff(const char *a, const char *b)
797 xbt_dynar_t da = xbt_str_split(a, "\n");
798 xbt_dynar_t db = xbt_str_split(b, "\n");
799 xbt_dynar_t common_sequence, common_suffix;
801 const char **vec_a, **vec_b;
806 struct subsequence *seqs_base, *seqs;
807 struct subsequence subseq;
813 /* Clean empty lines at the end of da and db */
815 if (len > 0 && a[len - 1] == '\n')
816 xbt_dynar_pop(da, NULL);
818 if (len > 0 && b[len - 1] == '\n')
819 xbt_dynar_pop(db, NULL);
821 /* Various initializations */
822 /* Assume that dynar's content is contiguous */
824 len_a = xbt_dynar_length(da);
825 vec_a = len_a ? xbt_dynar_get_ptr(da, 0) : NULL;
827 len_b = xbt_dynar_length(db);
828 vec_b = len_b ? xbt_dynar_get_ptr(db, 0) : NULL;
829 max = MAX(len_a, len_b) + 1;
830 fp_base = xbt_new(int, 2 * max + 1);
831 fp = fp_base + max; /* indexes in [-max..max] */
832 seqs_base = xbt_new(struct subsequence, 2 * max + 1);
833 seqs = seqs_base + max; /* indexes in [-max..max] */
834 common_sequence = xbt_dynar_new(sizeof(struct subsequence), NULL);
835 common_suffix = xbt_dynar_new(sizeof(struct subsequence), NULL);
837 /* Add a sentinel a the end of the sequence */
841 xbt_dynar_push(common_suffix, &subseq);
843 /* Compute the Longest Common Subsequence */
844 diff_easy_prefix(vec_a, &a0, &len_a, vec_b, &b0, &len_b, common_sequence);
845 diff_easy_suffix(vec_a, &a0, &len_a, vec_b, &b0, &len_b, common_suffix);
846 diff_compute_lcs(vec_a, a0, len_a, vec_b, b0, len_b, common_sequence, seqs, fp);
847 while (!xbt_dynar_is_empty(common_suffix)) {
848 xbt_dynar_pop(common_suffix, &subseq);
849 xbt_dynar_push(common_sequence, &subseq);
852 /* Build a Shortest Edit Script, and the final result */
853 diff = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
856 xbt_dynar_foreach(common_sequence, s, subseq) {
857 while (x < subseq.x) {
858 char *topush = bprintf("- %s", vec_a[x++]);
859 xbt_dynar_push_as(diff, char*, topush);
861 while (y < subseq.y) {
862 char *topush = bprintf("+ %s", vec_b[y++]);
863 xbt_dynar_push_as(diff, char*, topush);
865 while (x < subseq.x + subseq.len) {
866 char *topush = bprintf(" %s", vec_a[x++]);
867 xbt_dynar_push_as(diff, char*, topush);
871 res = xbt_str_join(diff, "\n");
877 xbt_dynar_free(&common_sequence);
878 xbt_dynar_free(&common_suffix);
879 xbt_dynar_free(&diff);
885 /** @brief creates a new string containing what can be read on a fd
888 char *xbt_str_from_file(FILE * file)
890 xbt_strbuff_t buff = xbt_strbuff_new();
893 memset(bread, 0, 1024);
895 while (!feof(file)) {
896 int got = fread(bread, 1, 1023, file);
898 xbt_strbuff_append(buff, bread);
902 xbt_strbuff_free_container(buff);
906 /* @brief Retrun 1 if string 'str' starts with string 'start'
908 * \param str a string
909 * \param start the string to search in str
911 * \return 1 if 'str' starts with 'start'
913 int xbt_str_start_with(const char* str, const char* start)
916 size_t l_str = strlen(str);
917 size_t l_start = strlen(start);
919 if(l_start > l_str) return 0;
921 for(i = 0; i< l_start; i++){
922 if(str[i] != start[i]) return 0;
931 #define mytest(name, input, expected) \
932 xbt_test_add(name); \
933 d=xbt_str_split_quoted(input); \
934 s=xbt_str_join(d,"XXX"); \
935 xbt_test_assert(!strcmp(s,expected),\
936 "Input (%s) leads to (%s) instead of (%s)", \
941 XBT_TEST_SUITE("xbt_str", "String Handling");
942 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
947 mytest("Empty", "", "");
948 mytest("Basic test", "toto tutu", "totoXXXtutu");
949 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u",
951 mytest("Protected space", "toto\\ tutu", "toto tutu");
952 mytest("Several spaces", "toto tutu", "totoXXXtutu");
953 mytest("LTriming", " toto tatu", "totoXXXtatu");
954 mytest("Triming", " toto tutu ", "totoXXXtutu");
955 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
956 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
957 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
958 mytest("Backslashed quotes", "\\'toto tutu\\' tata",
959 "'totoXXXtutu'XXXtata");
960 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata",
961 "toto 'tutuXXXtata");
965 #define mytest_str(name, input, separator, expected) \
966 xbt_test_add(name); \
967 d=xbt_str_split_str(input, separator); \
968 s=xbt_str_join(d,"XXX"); \
969 xbt_test_assert(!strcmp(s,expected),\
970 "Input (%s) leads to (%s) instead of (%s)", \
975 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
980 mytest_str("Empty string and separator", "", "", "");
981 mytest_str("Empty string", "", "##", "");
982 mytest_str("Empty separator", "toto", "", "toto");
983 mytest_str("String with no separator in it", "toto", "##", "toto");
984 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
987 /* Last args are format string and parameters for xbt_test_add */
988 #define mytest_diff(s1, s2, diff, ...) \
990 char *mytest_diff_res; \
991 xbt_test_add(__VA_ARGS__); \
992 mytest_diff_res = xbt_str_diff(s1, s2); \
993 xbt_test_assert(!strcmp(mytest_diff_res, diff), \
994 "Wrong output:\n--- got:\n%s\n--- expected:\n%s\n---", \
995 mytest_diff_res, diff); \
996 free(mytest_diff_res); \
999 XBT_TEST_UNIT("xbt_str_diff", test_diff, "test the function xbt_str_diff")
1004 mytest_diff("a", "a", " a", "1 word, no difference");
1005 mytest_diff("a", "A", "- a\n+ A", "1 word, different");
1006 mytest_diff("a\n", "a\n", " a", "1 line, no difference");
1007 mytest_diff("a\n", "A\n", "- a\n+ A", "1 line, different");
1010 mytest_diff("", "", "", "empty strings");
1011 mytest_diff("", "a", "+ a", "1 word, added");
1012 mytest_diff("a", "", "- a", "1 word, removed");
1013 mytest_diff("", "a\n", "+ a", "1 line, added");
1014 mytest_diff("a\n", "", "- a", "1 line, removed");
1015 mytest_diff("", "a\nb\nc\n", "+ a\n+ b\n+ c", "4 lines, all added");
1016 mytest_diff("a\nb\nc\n", "", "- a\n- b\n- c", "4 lines, all removed");
1019 mytest_diff("\n", "\n", " ", "empty lines");
1020 mytest_diff("", "\n", "+ ", "empty line, added");
1021 mytest_diff("\n", "", "- ", "empty line, removed");
1023 mytest_diff("a", "\na", "+ \n a", "empty line added before word");
1024 mytest_diff("a", "a\n\n", " a\n+ ", "empty line added after word");
1025 mytest_diff("\na", "a", "- \n a", "empty line removed before word");
1026 mytest_diff("a\n\n", "a", " a\n- ", "empty line removed after word");
1028 mytest_diff("a\n", "\na\n", "+ \n a", "empty line added before line");
1029 mytest_diff("a\n", "a\n\n", " a\n+ ", "empty line added after line");
1030 mytest_diff("\na\n", "a\n", "- \n a", "empty line removed before line");
1031 mytest_diff("a\n\n", "a\n", " a\n- ", "empty line removed after line");
1033 mytest_diff("a\nb\nc\nd\n", "\na\nb\nc\nd\n", "+ \n a\n b\n c\n d",
1034 "empty line added before 4 lines");
1035 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd\n\n", " a\n b\n c\n d\n+ ",
1036 "empty line added after 4 lines");
1037 mytest_diff("\na\nb\nc\nd\n", "a\nb\nc\nd\n", "- \n a\n b\n c\n d",
1038 "empty line removed before 4 lines");
1039 mytest_diff("a\nb\nc\nd\n\n", "a\nb\nc\nd\n", " a\n b\n c\n d\n- ",
1040 "empty line removed after 4 lines");
1042 /* Missing newline at the end of one of the strings */
1043 mytest_diff("a\n", "a", " a", "1 line, 1 word, no difference");
1044 mytest_diff("a", "a\n", " a", "1 word, 1 line, no difference");
1045 mytest_diff("a\n", "A", "- a\n+ A", "1 line, 1 word, different");
1046 mytest_diff("a", "A\n", "- a\n+ A", "1 word, 1 line, different");
1048 mytest_diff("a\nb\nc\nd", "a\nb\nc\nd\n", " a\n b\n c\n d",
1049 "4 lines, no newline on first");
1050 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd", " a\n b\n c\n d",
1051 "4 lines, no newline on second");
1053 /* Four lines, all combinations of differences */
1054 for (i = 0 ; i < (1U << 4) ; i++) {
1057 char res[4 * 8 + 1];
1064 for (/* j */ ; j < 4 && !(i & (1U << j)) ; j++) {
1066 ps += sprintf(ps, "%c\n", "abcd"[j]);
1067 pr += sprintf(pr, " %c\n", "abcd"[j]);
1069 for (k = j ; k < 4 && (i & (1U << k)) ; k++) {
1071 ps += sprintf(ps, "%c\n", "ABCD"[k]);
1072 pr += sprintf(pr, "- %c\n", "abcd"[k]);
1074 for (/* j */ ; j < k ; j++) {
1075 pr += sprintf(pr, "+ %c\n", "ABCD"[j]);
1079 *--pr = '\0'; /* strip last '\n' from expected result */
1080 mytest_diff("a\nb\nc\nd\n", s2, res,
1081 "compare (abcd) with changed (%s)", d2);
1084 /* Subsets of four lines, do not test for empty subset */
1085 for (i = 1 ; i < (1U << 4) ; i++) {
1088 char res[4 * 8 + 1];
1094 for (/* j */ ; j < 4 && (i & (1U << j)) ; j++) {
1096 ps += sprintf(ps, "%c\n", "abcd"[j]);
1097 pr += sprintf(pr, " %c\n", "abcd"[j]);
1099 for (/* j */; j < 4 && !(i & (1U << j)) ; j++) {
1100 pr += sprintf(pr, "- %c\n", "abcd"[j]);
1104 *--pr = '\0'; /* strip last '\n' from expected result */
1105 mytest_diff("a\nb\nc\nd\n", s2, res,
1106 "compare (abcd) with subset (%s)", d2);
1108 for (pr = res ; *pr != '\0' ; pr++)
1111 mytest_diff(s2, "a\nb\nc\nd\n", res,
1112 "compare subset (%s) with (abcd)", d2);
1116 #endif /* SIMGRID_TEST */