1 /* xbt_str.c - various helping functions to deal with strings */
3 /* Copyright (c) 2007-2014. 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. */
9 #include "src/portable.h"
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 Substitutes a char for another in a string
129 * @param str the string to modify
130 * @param from char to search
131 * @param to char to put instead
132 * @param occurence number of changes to do (=0 means all)
134 void xbt_str_subst(char *str, char from, char to, int occurence)
148 /** @brief Replaces a set of variables by their values
150 * @param str The input of the replacement process
151 * @param patterns The changes to apply
152 * @return The string modified
154 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
156 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
158 * You can provide a default value to use if the variable is not set in the dict by using
159 * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they
160 * shouldn't to respect the shell standard (:= form should set the value in the dict,
161 * but does not) (BUG).
164 char *xbt_str_varsubst(const char *str, xbt_dict_t patterns)
166 xbt_strbuff_t buff = xbt_strbuff_new_from(str);
168 xbt_strbuff_varsubst(buff, patterns);
170 xbt_strbuff_free_container(buff);
175 /** @brief Splits a string into a dynar of strings
177 * @param s: the string to split
178 * @param sep: a string of all chars to consider as separator.
180 * By default (with sep=NULL), these characters are used as separator:
182 * - " " (ASCII 32 (0x20)) space.
183 * - "\t" (ASCII 9 (0x09)) tab.
184 * - "\n" (ASCII 10 (0x0A)) line feed.
185 * - "\r" (ASCII 13 (0x0D)) carriage return.
186 * - "\0" (ASCII 0 (0x00)) NULL.
187 * - "\x0B" (ASCII 11 (0x0B)) vertical tab.
190 xbt_dynar_t xbt_str_split(const char *s, const char *sep)
192 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
195 const char *sep_dflt = " \t\n\r\x0B";
196 char is_sep[256] = { 1, 0 };
198 /* check what are the separators */
199 memset(is_sep, 0, sizeof(is_sep));
202 is_sep[(unsigned char) *sep_dflt++] = 1;
205 is_sep[(unsigned char) *sep++] = 1;
207 is_sep[0] = 1; /* End of string is also separator */
218 while (!is_sep[(unsigned char) *q]) {
224 topush = xbt_malloc(q - p + 1);
225 memcpy(topush, p, q - p);
226 topush[q - p] = '\0';
227 xbt_dynar_push(res, &topush);
235 * \brief This functions splits a string after using another string as separator
236 * For example A!!B!!C splitted after !! will return the dynar {A,B,C}
237 * \return An array of dynars containing the string tokens
239 xbt_dynar_t xbt_str_split_str(const char *s, const char *sep)
241 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
250 if (sep[0] == '\0') {
252 xbt_dynar_push(res, &s);
259 //get the start of the first occurence of the substring
261 //if substring was not found add the entire string
264 to_push = xbt_malloc(v + 1);
265 memcpy(to_push, p, v);
267 xbt_dynar_push(res, &to_push);
271 to_push = xbt_malloc(q - p + 1);
272 memcpy(to_push, p, q - p);
273 //add string terminator
274 to_push[q - p] = '\0';
275 xbt_dynar_push(res, &to_push);
282 /** @brief Just like @ref xbt_str_split_quoted (Splits a string into a dynar of strings), but without memory allocation
284 * The string passed as argument must be writable (not const)
285 * The elements of the dynar are just parts of the string passed as argument.
286 * So if you don't store that argument elsewhere, you should free it in addition
287 * to freeing the dynar. This can be done by simply freeing the first argument
289 * free(xbt_dynar_get_ptr(dynar,0));
291 * Actually this function puts a bunch of \0 in the memory area you passed as
292 * argument to separate the elements, and pushes the address of each chunk
293 * in the resulting dynar. Yes, that's uneven. Yes, that's gory. But that's efficient.
295 xbt_dynar_t xbt_str_split_quoted_in_place(char *s) {
296 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), NULL);
297 char *beg, *end; /* pointers around the parsed chunk */
298 int in_simple_quote = 0, in_double_quote = 0;
300 int ctn = 0; /* Got something in this block */
307 /* do not trim leading spaces: caller responsibility to clean his cruft */
316 /* Protected char; move it closer */
317 memmove(end, end + 1, strlen(end));
319 THROWF(arg_error, 0, "String ends with \\");
320 end++; /* Pass the protected char */
325 if (!in_double_quote) {
326 in_simple_quote = !in_simple_quote;
327 memmove(end, end + 1, strlen(end));
329 /* simple quote protected by double ones */
335 if (!in_simple_quote) {
336 in_double_quote = !in_double_quote;
337 memmove(end, end + 1, strlen(end));
339 /* double quote protected by simple ones */
348 if (*end == '\0' && (in_simple_quote || in_double_quote)) {
350 "End of string found while searching for %c in %s",
351 (in_simple_quote ? '\'' : '"'), s);
353 if (in_simple_quote || in_double_quote) {
361 /* Found a separator. Push the string if contains something */
362 xbt_dynar_push(res, &beg);
370 /* trim within the string, manually to speed things up */
385 /** @brief Splits a string into a dynar of strings, taking quotes into account
387 * It basically does the same argument separation than the shell, where white
388 * spaces can be escaped and where arguments are never split within a
390 * Several subsequent spaces are ignored (unless within quotes, of course).
391 * You may want to trim the input string, if you want to avoid empty entries
395 xbt_dynar_t xbt_str_split_quoted(const char *s)
397 xbt_dynar_t res = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
399 char *str_to_free; /* we have to copy the string before, to handle backslashes */
405 str_to_free = xbt_strdup(s);
407 parsed = xbt_str_split_quoted_in_place(str_to_free);
408 xbt_dynar_foreach(parsed,cursor,p) {
409 char *q=xbt_strdup(p);
410 xbt_dynar_push(res,&q);
413 xbt_dynar_shrink(res, 0);
414 xbt_dynar_free(&parsed);
418 /** @brief Join a set of strings as a single string */
419 char *xbt_str_join(xbt_dynar_t dyn, const char *sep)
421 int len = 1, dyn_len = xbt_dynar_length(dyn);
427 return xbt_strdup("");
429 /* compute the length */
430 xbt_dynar_foreach(dyn, cpt, cursor) {
431 len += strlen(cursor);
433 len += strlen(sep) * dyn_len;
435 res = xbt_malloc(len);
437 xbt_dynar_foreach(dyn, cpt, cursor) {
438 if ((int) cpt < dyn_len - 1)
439 p += sprintf(p, "%s%s", cursor, sep);
441 p += sprintf(p, "%s", cursor);
445 /** @brief Join a set of strings as a single string
447 * The parameter must be a NULL-terminated array of chars,
448 * just like xbt_dynar_to_array() produces
450 char *xbt_str_join_array(const char *const *strs, const char *sep)
453 int amount_strings=0;
457 if ((!strs) || (!strs[0]))
458 return xbt_strdup("");
460 /* compute the length before malloc */
461 for (i=0;strs[i];i++) {
462 len += strlen(strs[i]);
465 len += strlen(sep) * amount_strings;
468 q = res = xbt_malloc(len);
469 for (i=0;strs[i];i++) {
470 if (i!=0) { // not first loop
471 q += sprintf(q, "%s%s", sep, strs[i]);
473 q += sprintf(q,"%s",strs[i]);
480 * Diff related functions
482 * Implementation of the algorithm described in "An O(NP) Sequence Comparison
483 * Algorithm", by Sun Wu, Udi Manber, Gene Myers, and Webb Miller (Information
484 * Processing Letters 35(6):317-323, 1990), with the linear-space
485 * divide-and-conquer strategy described in "An O(ND) Difference Algorithm and
486 * Its Variations", by Eugene W. Myers (Algorithmica 1:251-266, 1986).
490 int x, y; /* starting coordinates */
491 int len; /* length */
495 void diff_snake(const char *vec_a[], int a0, int len_a,
496 const char *vec_b[], int b0, int len_b,
497 struct subsequence *seqs, int *fp, int k, int limit)
501 int fp_left = fp[k - 1] + 1;
502 int fp_right = fp[k + 1];
503 if (fp_left > fp_right) {
511 if (x + y <= limit) {
516 seqs[k] = seqs[record_seq];
518 while (x < len_a && y < len_b && !strcmp(vec_a[a0 + x], vec_b[b0 + y]))
522 seqs[k].len = x - seqs[k].x;
525 /* Returns the length of a shortest edit script, and a common
526 * subsequence from the middle.
528 static int diff_middle_subsequence(const char *vec_a[], int a0, int len_a,
529 const char *vec_b[], int b0, int len_b,
530 struct subsequence *subseq,
531 struct subsequence *seqs, int *fp)
533 const int delta = len_a - len_b;
534 const int limit = (len_a + len_b) / 2;
547 for (k = kmin; k <= kmax; k++)
551 fp[kmin - p - 1] = fp[kmax + p + 1] = -1;
552 for (k = kmax + p; k > delta; k--)
553 diff_snake(vec_a, a0, len_a, vec_b, b0, len_b, seqs, fp, k, limit);
554 for (k = kmin - p; k <= delta; k++)
555 diff_snake(vec_a, a0, len_a, vec_b, b0, len_b, seqs, fp, k, limit);
556 } while (fp[delta] != len_a);
558 subseq->x = a0 + seqs[delta].x;
559 subseq->y = b0 + seqs[delta].y;
560 subseq->len = seqs[delta].len;
561 return abs(delta) + 2 * p;;
564 /* Finds a longest common subsequence.
565 * Returns its length.
567 static int diff_compute_lcs(const char *vec_a[], int a0, int len_a,
568 const char *vec_b[], int b0, int len_b,
569 xbt_dynar_t common_sequence,
570 struct subsequence *seqs, int *fp)
572 if (len_a > 0 && len_b > 0) {
573 struct subsequence subseq;
574 int ses_len = diff_middle_subsequence(vec_a, a0, len_a, vec_b, b0, len_b,
576 int lcs_len = (len_a + len_b - ses_len) / 2;
579 } else if (ses_len > 1) {
580 int lcs_len1 = subseq.len;
581 if (lcs_len1 < lcs_len)
582 lcs_len1 += diff_compute_lcs(vec_a, a0, subseq.x - a0,
583 vec_b, b0, subseq.y - b0,
584 common_sequence, seqs, fp);
586 xbt_dynar_push(common_sequence, &subseq);
587 if (lcs_len1 < lcs_len) {
588 int u = subseq.x + subseq.len;
589 int v = subseq.y + subseq.len;
590 diff_compute_lcs(vec_a, u, a0 + len_a - u, vec_b, v, b0 + len_b - v,
591 common_sequence, seqs, fp);
594 int len = MIN(len_a, len_b) - subseq.len;
595 if (subseq.x == a0 && subseq.y == b0) {
597 xbt_dynar_push(common_sequence, &subseq);
599 struct subsequence subseq0 = {a0 + len_a - len,
600 b0 + len_b - len, len};
601 xbt_dynar_push(common_sequence, &subseq0);
605 struct subsequence subseq0 = {a0, b0, len};
606 xbt_dynar_push(common_sequence, &subseq0);
609 xbt_dynar_push(common_sequence, &subseq);
618 static int diff_member(const char *s, const char *vec[], int from, int to)
620 for ( ; from < to ; from++)
621 if (!strcmp(s, vec[from]))
626 /* Extract common prefix.
628 static void diff_easy_prefix(const char *vec_a[], int *a0_p, int *len_a_p,
629 const char *vec_b[], int *b0_p, int *len_b_p,
630 xbt_dynar_t common_sequence)
634 int len_a = *len_a_p;
635 int len_b = *len_b_p;
637 while (len_a > 0 && len_b > 0) {
638 struct subsequence subseq = {a0, b0, 0};
639 while (len_a > 0 && len_b > 0 && !strcmp(vec_a[a0], vec_b[b0])) {
645 xbt_dynar_push(common_sequence, &subseq);
646 if (len_a > 0 && len_b > 0 &&
647 !diff_member(vec_a[a0], vec_b, b0 + 1, b0 + len_b)) {
660 /* Extract common suffix.
662 static void diff_easy_suffix(const char *vec_a[], int *a0_p, int *len_a_p,
663 const char *vec_b[], int *b0_p, int *len_b_p,
664 xbt_dynar_t common_suffix)
668 int len_a = *len_a_p;
669 int len_b = *len_b_p;
671 while (len_a > 0 && len_b > 0){
672 struct subsequence subseq;
674 while (len_a > 0 && len_b > 0 &&
675 !strcmp(vec_a[a0 + len_a - 1], vec_b[b0 + len_b - 1])) {
680 if (subseq.len > 0) {
681 subseq.x = a0 + len_a;
682 subseq.y = b0 + len_b;
683 xbt_dynar_push(common_suffix, &subseq);
685 if (len_a > 0 && len_b > 0 &&
686 !diff_member(vec_b[b0 + len_b - 1], vec_a, a0, a0 + len_a - 1)) {
699 /** @brief Compute the unified diff of two strings */
700 char *xbt_str_diff(const char *a, const char *b)
702 xbt_dynar_t da = xbt_str_split(a, "\n");
703 xbt_dynar_t db = xbt_str_split(b, "\n");
704 xbt_dynar_t common_sequence, common_suffix;
706 const char **vec_a, **vec_b;
711 struct subsequence *seqs_base, *seqs;
712 struct subsequence subseq;
718 /* Clean empty lines at the end of da and db */
720 if (len > 0 && a[len - 1] == '\n')
721 xbt_dynar_pop(da, NULL);
723 if (len > 0 && b[len - 1] == '\n')
724 xbt_dynar_pop(db, NULL);
726 /* Various initializations */
727 /* Assume that dynar's content is contiguous */
729 len_a = xbt_dynar_length(da);
730 vec_a = len_a ? xbt_dynar_get_ptr(da, 0) : NULL;
732 len_b = xbt_dynar_length(db);
733 vec_b = len_b ? xbt_dynar_get_ptr(db, 0) : NULL;
734 max = MAX(len_a, len_b) + 1;
735 fp_base = xbt_new(int, 2 * max + 1);
736 fp = fp_base + max; /* indexes in [-max..max] */
737 seqs_base = xbt_new(struct subsequence, 2 * max + 1);
738 seqs = seqs_base + max; /* indexes in [-max..max] */
739 common_sequence = xbt_dynar_new(sizeof(struct subsequence), NULL);
740 common_suffix = xbt_dynar_new(sizeof(struct subsequence), NULL);
742 /* Add a sentinel a the end of the sequence */
746 xbt_dynar_push(common_suffix, &subseq);
748 /* Compute the Longest Common Subsequence */
749 diff_easy_prefix(vec_a, &a0, &len_a, vec_b, &b0, &len_b, common_sequence);
750 diff_easy_suffix(vec_a, &a0, &len_a, vec_b, &b0, &len_b, common_suffix);
751 diff_compute_lcs(vec_a, a0, len_a, vec_b, b0, len_b, common_sequence, seqs, fp);
752 while (!xbt_dynar_is_empty(common_suffix)) {
753 xbt_dynar_pop(common_suffix, &subseq);
754 xbt_dynar_push(common_sequence, &subseq);
757 /* Build a Shortest Edit Script, and the final result */
758 diff = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
761 xbt_dynar_foreach(common_sequence, s, subseq) {
762 while (x < subseq.x) {
763 char *topush = bprintf("- %s", vec_a[x++]);
764 xbt_dynar_push_as(diff, char*, topush);
766 while (y < subseq.y) {
767 char *topush = bprintf("+ %s", vec_b[y++]);
768 xbt_dynar_push_as(diff, char*, topush);
770 while (x < subseq.x + subseq.len) {
771 char *topush = bprintf(" %s", vec_a[x++]);
772 xbt_dynar_push_as(diff, char*, topush);
776 res = xbt_str_join(diff, "\n");
782 xbt_dynar_free(&common_sequence);
783 xbt_dynar_free(&common_suffix);
784 xbt_dynar_free(&diff);
790 /** @brief creates a new string containing what can be read on a fd
793 char *xbt_str_from_file(FILE * file)
795 xbt_strbuff_t buff = xbt_strbuff_new();
798 memset(bread, 0, 1024);
800 while (!feof(file)) {
801 int got = fread(bread, 1, 1023, file);
803 xbt_strbuff_append(buff, bread);
807 xbt_strbuff_free_container(buff);
811 /** @brief Parse an integer out of a string, or raise an error
813 * The #str is passed as argument to your #error_msg, as follows:
814 * THROWF(arg_error, 0, error_msg, str);
816 long int xbt_str_parse_int(const char* str, const char* error_msg)
819 if (str == NULL || str[0] == '\0')
820 THROWF(arg_error, 0, error_msg, str);
822 long int res = strtol(str, &endptr, 10);
823 if (endptr[0] != '\0')
824 THROWF(arg_error, 0, error_msg, str);
828 /** @brief Parse a double out of a string, or raise an error
830 * The #str is passed as argument to your #error_msg, as follows:
831 * THROWF(arg_error, 0, error_msg, str);
833 double xbt_str_parse_double(const char* str, const char* error_msg)
836 if (str == NULL || str[0] == '\0')
837 THROWF(arg_error, 0, error_msg, str);
839 double res = strtod(str, &endptr);
840 if (endptr[0] != '\0')
841 THROWF(arg_error, 0, error_msg, str);
849 XBT_TEST_SUITE("xbt_str", "String Handling");
851 #define mytest(name, input, expected) \
852 xbt_test_add(name); \
853 d=xbt_str_split_quoted(input); \
854 s=xbt_str_join(d,"XXX"); \
855 xbt_test_assert(!strcmp(s,expected),\
856 "Input (%s) leads to (%s) instead of (%s)", \
860 XBT_TEST_UNIT("xbt_str_split_quoted", test_split_quoted, "test the function xbt_str_split_quoted")
865 mytest("Empty", "", "");
866 mytest("Basic test", "toto tutu", "totoXXXtutu");
867 mytest("Useless backslashes", "\\t\\o\\t\\o \\t\\u\\t\\u",
869 mytest("Protected space", "toto\\ tutu", "toto tutu");
870 mytest("Several spaces", "toto tutu", "totoXXXtutu");
871 mytest("LTriming", " toto tatu", "totoXXXtatu");
872 mytest("Triming", " toto tutu ", "totoXXXtutu");
873 mytest("Single quotes", "'toto tutu' tata", "toto tutuXXXtata");
874 mytest("Double quotes", "\"toto tutu\" tata", "toto tutuXXXtata");
875 mytest("Mixed quotes", "\"toto' 'tutu\" tata", "toto' 'tutuXXXtata");
876 mytest("Backslashed quotes", "\\'toto tutu\\' tata",
877 "'totoXXXtutu'XXXtata");
878 mytest("Backslashed quotes + quotes", "'toto \\'tutu' tata",
879 "toto 'tutuXXXtata");
883 #define mytest_str(name, input, separator, expected) \
884 xbt_test_add(name); \
885 d=xbt_str_split_str(input, separator); \
886 s=xbt_str_join(d,"XXX"); \
887 xbt_test_assert(!strcmp(s,expected),\
888 "Input (%s) leads to (%s) instead of (%s)", \
893 XBT_TEST_UNIT("xbt_str_split_str", test_split_str, "test the function xbt_str_split_str")
898 mytest_str("Empty string and separator", "", "", "");
899 mytest_str("Empty string", "", "##", "");
900 mytest_str("Empty separator", "toto", "", "toto");
901 mytest_str("String with no separator in it", "toto", "##", "toto");
902 mytest_str("Basic test", "toto##tutu", "##", "totoXXXtutu");
905 /* Last args are format string and parameters for xbt_test_add */
906 #define mytest_diff(s1, s2, diff, ...) \
908 char *mytest_diff_res; \
909 xbt_test_add(__VA_ARGS__); \
910 mytest_diff_res = xbt_str_diff(s1, s2); \
911 xbt_test_assert(!strcmp(mytest_diff_res, diff), \
912 "Wrong output:\n--- got:\n%s\n--- expected:\n%s\n---", \
913 mytest_diff_res, diff); \
914 free(mytest_diff_res); \
917 XBT_TEST_UNIT("xbt_str_diff", test_diff, "test the function xbt_str_diff")
922 mytest_diff("a", "a", " a", "1 word, no difference");
923 mytest_diff("a", "A", "- a\n+ A", "1 word, different");
924 mytest_diff("a\n", "a\n", " a", "1 line, no difference");
925 mytest_diff("a\n", "A\n", "- a\n+ A", "1 line, different");
928 mytest_diff("", "", "", "empty strings");
929 mytest_diff("", "a", "+ a", "1 word, added");
930 mytest_diff("a", "", "- a", "1 word, removed");
931 mytest_diff("", "a\n", "+ a", "1 line, added");
932 mytest_diff("a\n", "", "- a", "1 line, removed");
933 mytest_diff("", "a\nb\nc\n", "+ a\n+ b\n+ c", "4 lines, all added");
934 mytest_diff("a\nb\nc\n", "", "- a\n- b\n- c", "4 lines, all removed");
937 mytest_diff("\n", "\n", " ", "empty lines");
938 mytest_diff("", "\n", "+ ", "empty line, added");
939 mytest_diff("\n", "", "- ", "empty line, removed");
941 mytest_diff("a", "\na", "+ \n a", "empty line added before word");
942 mytest_diff("a", "a\n\n", " a\n+ ", "empty line added after word");
943 mytest_diff("\na", "a", "- \n a", "empty line removed before word");
944 mytest_diff("a\n\n", "a", " a\n- ", "empty line removed after word");
946 mytest_diff("a\n", "\na\n", "+ \n a", "empty line added before line");
947 mytest_diff("a\n", "a\n\n", " a\n+ ", "empty line added after line");
948 mytest_diff("\na\n", "a\n", "- \n a", "empty line removed before line");
949 mytest_diff("a\n\n", "a\n", " a\n- ", "empty line removed after line");
951 mytest_diff("a\nb\nc\nd\n", "\na\nb\nc\nd\n", "+ \n a\n b\n c\n d",
952 "empty line added before 4 lines");
953 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd\n\n", " a\n b\n c\n d\n+ ",
954 "empty line added after 4 lines");
955 mytest_diff("\na\nb\nc\nd\n", "a\nb\nc\nd\n", "- \n a\n b\n c\n d",
956 "empty line removed before 4 lines");
957 mytest_diff("a\nb\nc\nd\n\n", "a\nb\nc\nd\n", " a\n b\n c\n d\n- ",
958 "empty line removed after 4 lines");
960 /* Missing newline at the end of one of the strings */
961 mytest_diff("a\n", "a", " a", "1 line, 1 word, no difference");
962 mytest_diff("a", "a\n", " a", "1 word, 1 line, no difference");
963 mytest_diff("a\n", "A", "- a\n+ A", "1 line, 1 word, different");
964 mytest_diff("a", "A\n", "- a\n+ A", "1 word, 1 line, different");
966 mytest_diff("a\nb\nc\nd", "a\nb\nc\nd\n", " a\n b\n c\n d",
967 "4 lines, no newline on first");
968 mytest_diff("a\nb\nc\nd\n", "a\nb\nc\nd", " a\n b\n c\n d",
969 "4 lines, no newline on second");
971 /* Four lines, all combinations of differences */
972 for (i = 0 ; i < (1U << 4) ; i++) {
982 for (/* j */ ; j < 4 && !(i & (1U << j)) ; j++) {
984 ps += sprintf(ps, "%c\n", "abcd"[j]);
985 pr += sprintf(pr, " %c\n", "abcd"[j]);
987 for (k = j ; k < 4 && (i & (1U << k)) ; k++) {
989 ps += sprintf(ps, "%c\n", "ABCD"[k]);
990 pr += sprintf(pr, "- %c\n", "abcd"[k]);
992 for (/* j */ ; j < k ; j++) {
993 pr += sprintf(pr, "+ %c\n", "ABCD"[j]);
997 *--pr = '\0'; /* strip last '\n' from expected result */
998 mytest_diff("a\nb\nc\nd\n", s2, res,
999 "compare (abcd) with changed (%s)", d2);
1002 /* Subsets of four lines, do not test for empty subset */
1003 for (i = 1 ; i < (1U << 4) ; i++) {
1006 char res[4 * 8 + 1];
1012 for (/* j */ ; j < 4 && (i & (1U << j)) ; j++) {
1014 ps += sprintf(ps, "%c\n", "abcd"[j]);
1015 pr += sprintf(pr, " %c\n", "abcd"[j]);
1017 for (/* j */; j < 4 && !(i & (1U << j)) ; j++) {
1018 pr += sprintf(pr, "- %c\n", "abcd"[j]);
1022 *--pr = '\0'; /* strip last '\n' from expected result */
1023 mytest_diff("a\nb\nc\nd\n", s2, res,
1024 "compare (abcd) with subset (%s)", d2);
1026 for (pr = res ; *pr != '\0' ; pr++)
1029 mytest_diff(s2, "a\nb\nc\nd\n", res,
1030 "compare subset (%s) with (abcd)", d2);
1034 #define test_parse_error(function, name, variable, str) \
1036 xbt_test_add(name); \
1038 variable = function(str, "Parse error"); \
1039 xbt_test_fail("The test '%s' did not detect the problem",name ); \
1041 if (e.category != arg_error) { \
1042 xbt_test_exception(e); \
1048 #define test_parse_ok(function, name, variable, str, value) \
1050 xbt_test_add(name); \
1052 variable = function(str, "Parse error"); \
1054 xbt_test_exception(e); \
1056 xbt_test_assert(variable == value, "Fail to parse '%s'", str); \
1059 XBT_TEST_UNIT("xbt_str_parse", test_parse, "Test the parsing functions")
1063 test_parse_ok(xbt_str_parse_int, "Parse int", rint, "42", 42);
1064 test_parse_ok(xbt_str_parse_int, "Parse 0 as an int", rint, "0", 0);
1065 test_parse_ok(xbt_str_parse_int, "Parse -1 as an int", rint, "-1", -1);
1067 test_parse_error(xbt_str_parse_int, "Parse int + noise", rint, "342 cruft");
1068 test_parse_error(xbt_str_parse_int, "Parse NULL as an int", rint, NULL);
1069 test_parse_error(xbt_str_parse_int, "Parse '' as an int", rint, "");
1070 test_parse_error(xbt_str_parse_int, "Parse cruft as an int", rint, "cruft");
1072 double rdouble = -9999;
1073 test_parse_ok(xbt_str_parse_double, "Parse 42 as a double", rdouble, "42", 42);
1074 test_parse_ok(xbt_str_parse_double, "Parse 42.5 as a double", rdouble, "42.5", 42.5);
1075 test_parse_ok(xbt_str_parse_double, "Parse 0 as a double", rdouble, "0", 0);
1076 test_parse_ok(xbt_str_parse_double, "Parse -1 as a double", rdouble, "-1", -1);
1078 test_parse_error(xbt_str_parse_double, "Parse double + noise", rdouble, "342 cruft");
1079 test_parse_error(xbt_str_parse_double, "Parse NULL as a double", rdouble, NULL);
1080 test_parse_error(xbt_str_parse_double, "Parse '' as a double", rdouble, "");
1081 test_parse_error(xbt_str_parse_double, "Parse cruft as a double", rdouble, "cruft");
1084 #endif /* SIMGRID_TEST */