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 static XBT_INLINE void diff_push(xbt_dynar_t dyn, char prefix, const char *s)
579 char *topush = bprintf("%c %s", prefix, s);
580 xbt_dynar_push(dyn, &topush);
583 static int diff_member(const char *s, xbt_dynar_t d, unsigned from, unsigned to)
586 for (i = from; i < to; i++)
587 if (!strcmp(s, xbt_dynar_get_as(d, i, char *)))
592 static void diff_easy_prefix(xbt_dynar_t res, xbt_dynar_t da, xbt_dynar_t db)
594 while (xbt_dynar_length(da) > 0 && xbt_dynar_length(db) > 0) {
595 char *sa = xbt_dynar_getfirst_as(da, char *);
596 char *sb = xbt_dynar_getfirst_as(db, char *);
598 if (!strcmp(sa, sb)) {
600 diff_push(res, ' ', sa);
601 xbt_dynar_shift(da, NULL);
602 xbt_dynar_shift(db, NULL);
603 } else if (!diff_member(sa, db, 1, xbt_dynar_length(db))) {
605 diff_push(res, '-', sa);
606 xbt_dynar_shift(da, NULL);
607 } else if (!diff_member(sb, da, 1, xbt_dynar_length(da))) {
609 diff_push(res, '+', sb);
610 xbt_dynar_shift(db, NULL);
612 /* sa in db, and sb in da: cannot go further */
618 static void diff_easy_suffix(xbt_dynar_t res, xbt_dynar_t da, xbt_dynar_t db)
620 while (xbt_dynar_length(da) > 0 && xbt_dynar_length(db) > 0) {
621 char *sa = xbt_dynar_getlast_as(da, char *);
622 char *sb = xbt_dynar_getlast_as(db, char *);
624 if (!strcmp(sa, sb)) {
626 diff_push(res, ' ', sa);
627 xbt_dynar_pop(da, NULL);
628 xbt_dynar_pop(db, NULL);
629 } else if (!diff_member(sb, da, 0, xbt_dynar_length(da) - 1)) {
631 diff_push(res, '+', sb);
632 xbt_dynar_pop(db, NULL);
633 } else if (!diff_member(sa, db, 0, xbt_dynar_length(db) - 1)) {
635 diff_push(res, '-', sa);
636 xbt_dynar_pop(da, NULL);
638 /* sa in db, and sb in da: cannot go further */
644 static XBT_INLINE int diff_get(xbt_matrix_t C, int i, int j)
646 return (i == -1 || j == -1) ? 0 : xbt_matrix_get_as(C, i, j, int);
649 static xbt_matrix_t diff_build_LCS(xbt_dynar_t da, xbt_dynar_t db)
651 unsigned len_a = xbt_dynar_length(da);
652 unsigned len_b = xbt_dynar_length(db);
653 xbt_matrix_t C = xbt_matrix_new(len_a, len_b, sizeof(int), NULL);
656 /* Compute the LCS */
658 function LCSLength(X[1..m], Y[1..n])
659 C = array(0..m, 0..n)
667 C[i,j] := C[i-1,j-1] + 1
669 C[i,j] := max(C[i,j-1], C[i-1,j])
672 for (i = 0; i < len_a; i++)
673 for (j = 0; j < len_b; j++) {
675 if (!strcmp(xbt_dynar_get_as(da, i, char *),
676 xbt_dynar_get_as(db, j, char *)))
677 xbt_matrix_get_as(C, i, j, int) = diff_get(C, i - 1, j - 1) + 1;
679 xbt_matrix_get_as(C, i, j, int) = max(diff_get(C, i, j - 1),
680 diff_get(C, i - 1, j));
685 static void diff_build_diff(xbt_dynar_t res,
687 xbt_dynar_t da, xbt_dynar_t db, int i, int j)
689 /* Construct the diff
690 function printDiff(C[0..m,0..n], X[1..m], Y[1..n], i, j)
691 if i > 0 and j > 0 and X[i] = Y[j]
692 printDiff(C, X, Y, i-1, j-1)
695 if j > 0 and (i = 0 or C[i,j-1] >= C[i-1,j])
696 printDiff(C, X, Y, i, j-1)
698 else if i > 0 and (j = 0 or C[i,j-1] < C[i-1,j])
699 printDiff(C, X, Y, i-1, j)
703 if (i >= 0 && j >= 0 && !strcmp(xbt_dynar_get_as(da, i, char *),
704 xbt_dynar_get_as(db, j, char *))) {
705 diff_build_diff(res, C, da, db, i - 1, j - 1);
706 diff_push(res, ' ', xbt_dynar_get_as(da, i, char *));
708 (i == -1 || diff_get(C, i, j - 1) >= diff_get(C, i - 1, j))) {
709 diff_build_diff(res, C, da, db, i, j - 1);
710 diff_push(res, '+', xbt_dynar_get_as(db, j, char *));
712 (j == -1 || diff_get(C, i, j - 1) < diff_get(C, i - 1, j))) {
713 diff_build_diff(res, C, da, db, i - 1, j);
714 diff_push(res, '-', xbt_dynar_get_as(da, i, char *));
718 /** @brief Compute the unified diff of two strings */
719 char *xbt_str_diff(const char *a, const char *b)
721 xbt_dynar_t da = xbt_str_split(a, "\n");
722 xbt_dynar_t db = xbt_str_split(b, "\n");
729 diff = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
730 diff2 = xbt_dynar_new(sizeof(char *), NULL);
732 /* Clean empty lines at the end of da and db */
734 if (len > 0 && a[len - 1] == '\n')
735 xbt_dynar_pop(da, NULL);
737 if (len > 0 && b[len - 1] == '\n')
738 xbt_dynar_pop(db, NULL);
740 /* Extract the easy suffix, do it before extracting the prefix,
741 * as xbt_dynar_pop costs less than xbt_dynar_shift */
742 diff_easy_suffix(diff2, da, db);
744 /* Extract the easy prefix */
745 diff_easy_prefix(diff, da, db);
747 /* Compute the diff for the remaining */
748 C = diff_build_LCS(da, db);
749 diff_build_diff(diff, C, da, db,
750 xbt_dynar_length(da) - 1, xbt_dynar_length(db) - 1);
755 /* Add the easy suffix, in reverse order */
756 while (!xbt_dynar_is_empty(diff2)) {
757 char *topush = xbt_dynar_pop_as(diff2, char *);
758 xbt_dynar_push(diff, &topush);
760 xbt_dynar_free(&diff2);
762 /* Build the final result */
763 res = xbt_str_join(diff, "\n");
764 xbt_dynar_free(&diff);
770 /** @brief creates a new string containing what can be read on a fd
773 char *xbt_str_from_file(FILE * file)
775 xbt_strbuff_t buff = xbt_strbuff_new();
778 memset(bread, 0, 1024);
780 while (!feof(file)) {
781 int got = fread(bread, 1, 1023, file);
783 xbt_strbuff_append(buff, bread);
787 xbt_strbuff_free_container(buff);
794 #define mytest(name, input, expected) \
795 xbt_test_add(name); \
796 d=xbt_str_split_quoted(input); \
797 s=xbt_str_join(d,"XXX"); \
798 xbt_test_assert(!strcmp(s,expected),\
799 "Input (%s) leads to (%s) instead of (%s)", \
804 XBT_TEST_SUITE("xbt_str", "String Handling");
805 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
810 mytest("Empty", "", "");
811 mytest("Basic test", "toto tutu", "totoXXXtutu");
812 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u",
814 mytest("Protected space", "toto\\ tutu", "toto tutu");
815 mytest("Several spaces", "toto tutu", "totoXXXtutu");
816 mytest("LTriming", " toto tatu", "totoXXXtatu");
817 mytest("Triming", " toto tutu ", "totoXXXtutu");
818 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
819 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
820 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
821 mytest("Backslashed quotes", "\\'toto tutu\\' tata",
822 "'totoXXXtutu'XXXtata");
823 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata",
824 "toto 'tutuXXXtata");
828 #define mytest_str(name, input, separator, expected) \
829 xbt_test_add(name); \
830 d=xbt_str_split_str(input, separator); \
831 s=xbt_str_join(d,"XXX"); \
832 xbt_test_assert(!strcmp(s,expected),\
833 "Input (%s) leads to (%s) instead of (%s)", \
838 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
843 mytest_str("Empty string and separator", "", "", "");
844 mytest_str("Empty string", "", "##", "");
845 mytest_str("Empty separator", "toto", "", "toto");
846 mytest_str("String with no separator in it", "toto", "##", "toto");
847 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
850 /* Last args are format string and parameters for xbt_test_add */
851 #define mytest_diff(s1, s2, diff, ...) \
853 char *mytest_diff_res; \
854 xbt_test_add(__VA_ARGS__); \
855 mytest_diff_res = xbt_str_diff(s1, s2); \
856 xbt_test_assert(!strcmp(mytest_diff_res, diff), \
857 "Wrong output:\n--- got:\n%s\n--- expected:\n%s\n---", \
858 mytest_diff_res, diff); \
859 free(mytest_diff_res); \
862 XBT_TEST_UNIT("xbt_str_diff", test_diff, "test the function xbt_str_diff")
867 mytest_diff("a", "a", " a", "1 word, no difference");
868 mytest_diff("a", "A", "- a\n+ A", "1 word, different");
869 mytest_diff("a\n", "a\n", " a", "1 line, no difference");
870 mytest_diff("a\n", "A\n", "- a\n+ A", "1 line, different");
873 mytest_diff("", "", "", "empty strings");
874 mytest_diff("", "a", "+ a", "1 word, added");
875 mytest_diff("a", "", "- a", "1 word, removed");
876 mytest_diff("", "a\n", "+ a", "1 line, added");
877 mytest_diff("a\n", "", "- a", "1 line, removed");
878 mytest_diff("", "a\nb\nc\n", "+ a\n+ b\n+ c", "4 lines, all added");
879 mytest_diff("a\nb\nc\n", "", "- a\n- b\n- c", "4 lines, all removed");
882 mytest_diff("\n", "\n", " ", "empty lines");
883 mytest_diff("", "\n", "+ ", "empty line, added");
884 mytest_diff("\n", "", "- ", "empty line, removed");
886 mytest_diff("a", "\na", "+ \n a", "empty line added before word");
887 mytest_diff("a", "a\n\n", " a\n+ ", "empty line added after word");
888 mytest_diff("\na", "a", "- \n a", "empty line removed before word");
889 mytest_diff("a\n\n", "a", " a\n- ", "empty line removed after word");
891 mytest_diff("a\n", "\na\n", "+ \n a", "empty line added before line");
892 mytest_diff("a\n", "a\n\n", " a\n+ ", "empty line added after line");
893 mytest_diff("\na\n", "a\n", "- \n a", "empty line removed before line");
894 mytest_diff("a\n\n", "a\n", " a\n- ", "empty line removed after line");
896 mytest_diff("a\nb\nc\nd\n", "\na\nb\nc\nd\n", "+ \n a\n b\n c\n d",
897 "empty line added before 4 lines");
898 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd\n\n", " a\n b\n c\n d\n+ ",
899 "empty line added after 4 lines");
900 mytest_diff("\na\nb\nc\nd\n", "a\nb\nc\nd\n", "- \n a\n b\n c\n d",
901 "empty line removed before 4 lines");
902 mytest_diff("a\nb\nc\nd\n\n", "a\nb\nc\nd\n", " a\n b\n c\n d\n- ",
903 "empty line removed after 4 lines");
905 /* Missing newline at the end of one of the strings */
906 mytest_diff("a\n", "a", " a", "1 line, 1 word, no difference");
907 mytest_diff("a", "a\n", " a", "1 word, 1 line, no difference");
908 mytest_diff("a\n", "A", "- a\n+ A", "1 line, 1 word, different");
909 mytest_diff("a", "A\n", "- a\n+ A", "1 word, 1 line, different");
911 mytest_diff("a\nb\nc\nd", "a\nb\nc\nd\n", " a\n b\n c\n d",
912 "4 lines, no newline on first");
913 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd", " a\n b\n c\n d",
914 "4 lines, no newline on second");
916 /* Four lines, all combinations of differences */
917 for (i = 0 ; i < (1U << 4) ; i++) {
927 for (/* j */ ; j < 4 && !(i & (1U << j)) ; j++) {
929 ps += sprintf(ps, "%c\n", "abcd"[j]);
930 pr += sprintf(pr, " %c\n", "abcd"[j]);
932 for (k = j ; k < 4 && (i & (1U << k)) ; k++) {
934 ps += sprintf(ps, "%c\n", "ABCD"[k]);
935 pr += sprintf(pr, "- %c\n", "abcd"[k]);
937 for (/* j */ ; j < k ; j++) {
938 pr += sprintf(pr, "+ %c\n", "ABCD"[j]);
942 *--pr = '\0'; /* strip last '\n' from expected result */
943 mytest_diff("a\nb\nc\nd\n", s2, res,
944 "compare (abcd) with changed (%s)", d2);
947 /* Subsets of four lines, do not test for empty subset */
948 for (i = 1 ; i < (1U << 4) ; i++) {
957 for (/* j */ ; j < 4 && (i & (1U << j)) ; j++) {
959 ps += sprintf(ps, "%c\n", "abcd"[j]);
960 pr += sprintf(pr, " %c\n", "abcd"[j]);
962 for (/* j */; j < 4 && !(i & (1U << j)) ; j++) {
963 pr += sprintf(pr, "- %c\n", "abcd"[j]);
967 *--pr = '\0'; /* strip last '\n' from expected result */
968 mytest_diff("a\nb\nc\nd\n", s2, res,
969 "compare (abcd) with subset (%s)", d2);
971 for (pr = res ; *pr != '\0' ; pr++)
974 mytest_diff(s2, "a\nb\nc\nd\n", res,
975 "compare subset (%s) with (abcd)", d2);
979 #endif /* SIMGRID_TEST */