1 /*************************************************************************
5 * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
13 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
14 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
16 ************************************************************************/
18 /*************************************************************************
30 /*************************************************************************
34 #if !defined(TRIO_STRING_PUBLIC)
35 # define TRIO_STRING_PUBLIC TRIO_PUBLIC
37 #if !defined(TRIO_STRING_PRIVATE)
38 # define TRIO_STRING_PRIVATE TRIO_PRIVATE
45 # define NIL ((char)0)
48 # define FALSE (1 == 0)
49 # define TRUE (! FALSE)
51 #if !defined(BOOLEAN_T)
52 # define BOOLEAN_T int
55 #if defined(TRIO_COMPILER_SUPPORTS_C99)
58 #elif defined(TRIO_COMPILER_MSVC)
62 #if defined(TRIO_PLATFORM_UNIX)
63 # define USE_STRCASECMP
64 # define USE_STRNCASECMP
66 # if defined(TRIO_PLATFORM_QNX)
67 # define strcasecmp(x,y) stricmp(x,y)
68 # define strncasecmp(x,y,n) strnicmp(x,y,n)
70 #elif defined(TRIO_PLATFORM_WIN32)
71 # define USE_STRCASECMP
72 # define strcasecmp(x,y) strcmpi(x,y)
75 /*************************************************************************
86 /*************************************************************************
90 #if !defined(TRIO_MINIMAL)
91 static TRIO_CONST char rcsid[] = "@(#)$Id$";
94 /*************************************************************************
95 * Static String Functions
98 #if defined(TRIO_DOCUMENTATION)
99 # include "doc/doc_static.h"
101 /** @addtogroup StaticStrings
108 @param size Size of new string.
109 @return Pointer to string, or NULL if allocation failed.
111 TRIO_STRING_PUBLIC char *
116 return (char *)TRIO_MALLOC(size);
123 @param string String to be freed.
125 TRIO_STRING_PUBLIC void
138 Count the number of characters in a string.
140 @param string String to measure.
141 @return Number of characters in @string.
143 TRIO_STRING_PUBLIC size_t
146 TRIO_CONST char *string)
148 return strlen(string);
152 #if !defined(TRIO_MINIMAL)
154 Append @p source at the end of @p target.
156 @param target Target string.
157 @param source Source string.
158 @return Boolean value indicating success or failure.
160 @pre @p target must point to a memory chunk with sufficient room to
161 contain the @p target string and @p source string.
162 @pre No boundary checking is performed, so insufficient memory will
163 result in a buffer overrun.
164 @post @p target will be zero terminated.
166 TRIO_STRING_PUBLIC int
168 TRIO_ARGS2((target, source),
170 TRIO_CONST char *source)
175 return (strcat(target, source) != NULL);
177 #endif /* !defined(TRIO_MINIMAL) */
179 #if !defined(TRIO_MINIMAL)
181 Append at most @p max characters from @p source to @p target.
183 @param target Target string.
184 @param max Maximum number of characters to append.
185 @param source Source string.
186 @return Boolean value indicating success or failure.
188 @pre @p target must point to a memory chuck with sufficient room to
189 contain the @p target string and the @p source string (at most @p max
191 @pre No boundary checking is performed, so insufficient memory will
192 result in a buffer overrun.
193 @post @p target will be zero terminated.
195 TRIO_STRING_PUBLIC int
197 TRIO_ARGS3((target, max, source),
200 TRIO_CONST char *source)
207 length = trio_length(target);
211 strncat(target, source, max - length - 1);
215 #endif /* !defined(TRIO_MINIMAL) */
218 #if !defined(TRIO_MINIMAL)
220 Determine if a string contains a substring.
222 @param string String to be searched.
223 @param substring String to be found.
224 @return Boolean value indicating success or failure.
226 TRIO_STRING_PUBLIC int
228 TRIO_ARGS2((string, substring),
229 TRIO_CONST char *string,
230 TRIO_CONST char *substring)
235 return (0 != strstr(string, substring));
237 #endif /* !defined(TRIO_MINIMAL) */
240 #if !defined(TRIO_MINIMAL)
242 Copy @p source to @p target.
244 @param target Target string.
245 @param source Source string.
246 @return Boolean value indicating success or failure.
248 @pre @p target must point to a memory chunk with sufficient room to
249 contain the @p source string.
250 @pre No boundary checking is performed, so insufficient memory will
251 result in a buffer overrun.
252 @post @p target will be zero terminated.
254 TRIO_STRING_PUBLIC int
256 TRIO_ARGS2((target, source),
258 TRIO_CONST char *source)
263 (void)strcpy(target, source);
266 #endif /* !defined(TRIO_MINIMAL) */
270 Copy at most @p max characters from @p source to @p target.
272 @param target Target string.
273 @param max Maximum number of characters to append.
274 @param source Source string.
275 @return Boolean value indicating success or failure.
277 @pre @p target must point to a memory chunk with sufficient room to
278 contain the @p source string (at most @p max characters).
279 @pre No boundary checking is performed, so insufficient memory will
280 result in a buffer overrun.
281 @post @p target will be zero terminated.
283 TRIO_STRING_PUBLIC int
285 TRIO_ARGS3((target, max, source),
288 TRIO_CONST char *source)
292 assert(max > 0); /* Includes != 0 */
294 (void)strncpy(target, source, max - 1);
295 target[max - 1] = (char)0;
303 TRIO_STRING_PRIVATE char *
305 TRIO_ARGS2((source, size),
306 TRIO_CONST char *source,
313 /* Make room for string plus a terminating zero */
315 target = trio_create(size);
318 trio_copy_max(target, size, source);
327 @param source Source string.
328 @return A copy of the @p source string.
330 @post @p target will be zero terminated.
332 TRIO_STRING_PUBLIC char *
335 TRIO_CONST char *source)
337 return TrioDuplicateMax(source, trio_length(source));
341 #if !defined(TRIO_MINIMAL)
343 Duplicate at most @p max characters of @p source.
345 @param source Source string.
346 @param max Maximum number of characters to duplicate.
347 @return A copy of the @p source string.
349 @post @p target will be zero terminated.
351 TRIO_STRING_PUBLIC char *
352 trio_duplicate_max TRIO_ARGS2((source, max),
353 TRIO_CONST char *source,
361 length = trio_length(source);
366 return TrioDuplicateMax(source, length);
368 #endif /* !defined(TRIO_MINIMAL) */
372 Compare if two strings are equal.
374 @param first First string.
375 @param second Second string.
376 @return Boolean indicating whether the two strings are equal or not.
378 Case-insensitive comparison.
380 TRIO_STRING_PUBLIC int
382 TRIO_ARGS2((first, second),
383 TRIO_CONST char *first,
384 TRIO_CONST char *second)
389 if ((first != NULL) && (second != NULL))
391 #if defined(USE_STRCASECMP)
392 return (0 == strcasecmp(first, second));
394 while ((*first != NIL) && (*second != NIL))
396 if (toupper(*first) != toupper(*second))
403 return ((*first == NIL) && (*second == NIL));
411 Compare if two strings are equal.
413 @param first First string.
414 @param second Second string.
415 @return Boolean indicating whether the two strings are equal or not.
417 Case-sensitive comparison.
419 TRIO_STRING_PUBLIC int
421 TRIO_ARGS2((first, second),
422 TRIO_CONST char *first,
423 TRIO_CONST char *second)
428 if ((first != NULL) && (second != NULL))
430 return (0 == strcmp(first, second));
436 #if !defined(TRIO_MINIMAL)
438 Compare if two strings up until the first @p max characters are equal.
440 @param first First string.
441 @param max Maximum number of characters to compare.
442 @param second Second string.
443 @return Boolean indicating whether the two strings are equal or not.
445 Case-sensitive comparison.
447 TRIO_STRING_PUBLIC int
449 TRIO_ARGS3((first, max, second),
450 TRIO_CONST char *first,
452 TRIO_CONST char *second)
457 if ((first != NULL) && (second != NULL))
459 return (0 == strncmp(first, second, max));
463 #endif /* !defined(TRIO_MINIMAL) */
467 Compare if two strings are equal.
469 @param first First string.
470 @param second Second string.
471 @return Boolean indicating whether the two strings are equal or not.
473 Collating characters are considered equal.
475 TRIO_STRING_PUBLIC int
477 TRIO_ARGS2((first, second),
478 TRIO_CONST char *first,
479 TRIO_CONST char *second)
484 #if defined(LC_COLLATE)
485 return (strcoll(first, second) == 0);
487 return trio_equal(first, second);
493 Compare if two strings up until the first @p max characters are equal.
495 @param first First string.
496 @param max Maximum number of characters to compare.
497 @param second Second string.
498 @return Boolean indicating whether the two strings are equal or not.
500 Case-insensitive comparison.
502 TRIO_STRING_PUBLIC int
504 TRIO_ARGS3((first, max, second),
505 TRIO_CONST char *first,
507 TRIO_CONST char *second)
512 if ((first != NULL) && (second != NULL))
514 #if defined(USE_STRNCASECMP)
515 return (0 == strncasecmp(first, second, max));
517 /* Not adequately tested yet */
519 while ((*first != NIL) && (*second != NIL) && (cnt <= max))
521 if (toupper(*first) != toupper(*second))
529 return ((cnt == max) || ((*first == NIL) && (*second == NIL)));
537 Provide a textual description of an error code (errno).
539 @param error_number Error number.
540 @return Textual description of @p error_number.
542 TRIO_STRING_PUBLIC TRIO_CONST char *
544 TRIO_ARGS1((error_number),
547 #if defined(USE_STRERROR)
548 return strerror(error_number);
555 #if !defined(TRIO_MINIMAL)
557 Format the date/time according to @p format.
559 @param target Target string.
560 @param max Maximum number of characters to format.
561 @param format Formatting string.
562 @param datetime Date/time structure.
563 @return Number of formatted characters.
565 The formatting string accepts the same specifiers as the standard C
568 TRIO_STRING_PUBLIC size_t
570 TRIO_ARGS4((target, max, format, datetime),
573 TRIO_CONST char *format,
574 TRIO_CONST struct tm *datetime)
581 return strftime(target, max, format, datetime);
583 #endif /* !defined(TRIO_MINIMAL) */
586 #if !defined(TRIO_MINIMAL)
588 Calculate a hash value for a string.
590 @param string String to be calculated on.
591 @param type Hash function.
592 @return Calculated hash value.
594 @p type can be one of the following
595 @li @c TRIO_HASH_PLAIN Plain hash function.
597 TRIO_STRING_PUBLIC unsigned long
599 TRIO_ARGS2((string, type),
600 TRIO_CONST char *string,
603 unsigned long value = 0L;
610 case TRIO_HASH_PLAIN:
611 while ( (ch = *string++) != NIL )
614 value += (unsigned long)ch;
623 #endif /* !defined(TRIO_MINIMAL) */
626 #if !defined(TRIO_MINIMAL)
628 Find first occurrence of a character in a string.
630 @param string String to be searched.
631 @param character Character to be found.
632 @param A pointer to the found character, or NULL if character was not found.
634 TRIO_STRING_PUBLIC char *
636 TRIO_ARGS2((string, character),
637 TRIO_CONST char *string,
642 return strchr(string, character);
644 #endif /* !defined(TRIO_MINIMAL) */
647 #if !defined(TRIO_MINIMAL)
649 Find last occurrence of a character in a string.
651 @param string String to be searched.
652 @param character Character to be found.
653 @param A pointer to the found character, or NULL if character was not found.
655 TRIO_STRING_PUBLIC char *
657 TRIO_ARGS2((string, character),
658 TRIO_CONST char *string,
663 return strchr(string, character);
665 #endif /* !defined(TRIO_MINIMAL) */
668 #if !defined(TRIO_MINIMAL)
670 Convert the alphabetic letters in the string to lower-case.
672 @param target String to be converted.
673 @return Number of processed characters (converted or not).
675 TRIO_STRING_PUBLIC int
682 return trio_span_function(target, target, tolower);
684 #endif /* !defined(TRIO_MINIMAL) */
687 #if !defined(TRIO_MINIMAL)
689 Compare two strings using wildcards.
691 @param string String to be searched.
692 @param pattern Pattern, including wildcards, to search for.
693 @return Boolean value indicating success or failure.
695 Case-insensitive comparison.
697 The following wildcards can be used
698 @li @c * Match any number of characters.
699 @li @c ? Match a single character.
701 TRIO_STRING_PUBLIC int
703 TRIO_ARGS2((string, pattern),
704 TRIO_CONST char *string,
705 TRIO_CONST char *pattern)
710 for (; ('*' != *pattern); ++pattern, ++string)
714 return (NIL == *pattern);
716 if ((toupper((int)*string) != toupper((int)*pattern))
717 && ('?' != *pattern))
722 /* two-line patch to prevent *too* much recursiveness: */
723 while ('*' == pattern[1])
728 if ( trio_match(string, &pattern[1]) )
737 #endif /* !defined(TRIO_MINIMAL) */
740 #if !defined(TRIO_MINIMAL)
742 Compare two strings using wildcards.
744 @param string String to be searched.
745 @param pattern Pattern, including wildcards, to search for.
746 @return Boolean value indicating success or failure.
748 Case-sensitive comparison.
750 The following wildcards can be used
751 @li @c * Match any number of characters.
752 @li @c ? Match a single character.
754 TRIO_STRING_PUBLIC int
756 TRIO_ARGS2((string, pattern),
757 TRIO_CONST char *string,
758 TRIO_CONST char *pattern)
763 for (; ('*' != *pattern); ++pattern, ++string)
767 return (NIL == *pattern);
769 if ((*string != *pattern)
770 && ('?' != *pattern))
775 /* two-line patch to prevent *too* much recursiveness: */
776 while ('*' == pattern[1])
781 if ( trio_match_case(string, &pattern[1]) )
790 #endif /* !defined(TRIO_MINIMAL) */
793 #if !defined(TRIO_MINIMAL)
795 Execute a function on each character in string.
797 @param target Target string.
798 @param source Source string.
799 @param Function Function to be executed.
800 @return Number of processed characters.
802 TRIO_STRING_PUBLIC size_t
804 TRIO_ARGS3((target, source, Function),
806 TRIO_CONST char *source,
807 int (*Function) TRIO_PROTO((int)))
815 while (*source != NIL)
817 *target++ = Function(*source++);
822 #endif /* !defined(TRIO_MINIMAL) */
825 #if !defined(TRIO_MINIMAL)
827 Search for a substring in a string.
829 @param string String to be searched.
830 @param substring String to be found.
831 @return Pointer to first occurrence of @p substring in @p string, or NULL
832 if no match was found.
834 TRIO_STRING_PUBLIC char *
836 TRIO_ARGS2((string, substring),
837 TRIO_CONST char *string,
838 TRIO_CONST char *substring)
843 return strstr(string, substring);
845 #endif /* !defined(TRIO_MINIMAL) */
848 #if !defined(TRIO_MINIMAL)
850 Search for a substring in the first @p max characters of a string.
852 @param string String to be searched.
853 @param max Maximum characters to be searched.
854 @param substring String to be found.
855 @return Pointer to first occurrence of @p substring in @p string, or NULL
856 if no match was found.
858 TRIO_STRING_PUBLIC char *
860 TRIO_ARGS3((string, max, substring),
861 TRIO_CONST char *string,
863 TRIO_CONST char *substring)
872 size = trio_length(substring);
875 for (count = 0; count <= max - size; count++)
877 if (trio_equal_max(substring, size, &string[count]))
879 result = (char *)&string[count];
886 #endif /* !defined(TRIO_MINIMAL) */
889 #if !defined(TRIO_MINIMAL)
893 @param string String to be tokenized.
894 @param tokens String containing list of delimiting characters.
895 @return Start of new token.
897 @warning @p string will be destroyed.
899 TRIO_STRING_PUBLIC char *
901 TRIO_ARGS2((string, delimiters),
903 TRIO_CONST char *delimiters)
907 return strtok(string, delimiters);
909 #endif /* !defined(TRIO_MINIMAL) */
913 Convert string to floating-point number.
915 @param source String to be converted.
916 @param endp Pointer to end of the converted string.
917 @return A floating-point number.
919 The following Extended Backus-Naur form is used
921 double ::= [ <sign> ]
923 <number> <decimal_point> <number> |
924 <decimal_point> <number> )
925 [ <exponential> [ <sign> ] <number> ]
926 number ::= 1*( <digit> )
927 digit ::= ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )
928 exponential ::= ( 'e' | 'E' )
929 sign ::= ( '-' | '+' )
930 decimal_point ::= '.'
933 /* FIXME: Add EBNF for hex-floats */
934 TRIO_STRING_PUBLIC trio_long_double_t
936 TRIO_ARGS2((source, endp),
937 TRIO_CONST char *source,
940 #if defined(USE_STRTOLD)
941 return strtold(source, endp);
943 int isNegative = FALSE;
944 int isExponentNegative = FALSE;
945 trio_long_double_t integer = 0.0;
946 trio_long_double_t fraction = 0.0;
947 unsigned long exponent = 0;
948 trio_long_double_t base;
949 trio_long_double_t fracdiv = 1.0;
950 trio_long_double_t value = 0.0;
952 /* First try hex-floats */
953 if ((source[0] == '0') && ((source[1] == 'x') || (source[1] == 'X')))
957 while (isxdigit((int)*source))
960 integer += (isdigit((int)*source)
962 : 10 + (toupper((int)*source) - 'A'));
968 while (isxdigit((int)*source))
971 fraction += fracdiv * (isdigit((int)*source)
973 : 10 + (toupper((int)*source) - 'A'));
976 if ((*source == 'p') || (*source == 'P'))
979 if ((*source == '+') || (*source == '-'))
981 isExponentNegative = (*source == '-');
984 while (isdigit((int)*source))
986 exponent *= (int)base;
987 exponent += (*source - '0');
993 else /* Then try normal decimal floats */
996 isNegative = (*source == '-');
998 if ((*source == '+') || (*source == '-'))
1002 while (isdigit((int)*source))
1005 integer += (*source - '0');
1011 source++; /* skip decimal point */
1012 while (isdigit((int)*source))
1015 fraction += (*source - '0') * fracdiv;
1019 if ((*source == 'e')
1027 source++; /* Skip exponential indicator */
1028 isExponentNegative = (*source == '-');
1029 if ((*source == '+') || (*source == '-'))
1031 while (isdigit((int)*source))
1033 exponent *= (int)base;
1034 exponent += (*source - '0');
1040 value = integer + fraction;
1043 if (isExponentNegative)
1044 value /= pow(base, (double)exponent);
1046 value *= pow(base, (double)exponent);
1052 *endp = (char *)source;
1059 Convert string to floating-point number.
1061 @param source String to be converted.
1062 @param endp Pointer to end of the converted string.
1063 @return A floating-point number.
1065 See @ref trio_to_long_double.
1067 TRIO_STRING_PUBLIC double
1069 TRIO_ARGS2((source, endp),
1070 TRIO_CONST char *source,
1073 #if defined(USE_STRTOD)
1074 return strtod(source, endp);
1076 return (double)trio_to_long_double(source, endp);
1080 #if !defined(TRIO_MINIMAL)
1082 Convert string to floating-point number.
1084 @param source String to be converted.
1085 @param endp Pointer to end of the converted string.
1086 @return A floating-point number.
1088 See @ref trio_to_long_double.
1090 TRIO_STRING_PUBLIC float
1092 TRIO_ARGS2((source, endp),
1093 TRIO_CONST char *source,
1096 #if defined(USE_STRTOF)
1097 return strtof(source, endp);
1099 return (float)trio_to_long_double(source, endp);
1102 #endif /* !defined(TRIO_MINIMAL) */
1106 Convert string to signed integer.
1108 @param string String to be converted.
1109 @param endp Pointer to end of converted string.
1110 @param base Radix number of number.
1112 TRIO_STRING_PUBLIC long
1114 TRIO_ARGS3((string, endp, base),
1115 TRIO_CONST char *string,
1120 assert((base >= 2) && (base <= 36));
1122 return strtol(string, endp, base);
1126 #if !defined(TRIO_MINIMAL)
1128 Convert string to unsigned integer.
1130 @param string String to be converted.
1131 @param endp Pointer to end of converted string.
1132 @param base Radix number of number.
1134 TRIO_STRING_PUBLIC unsigned long
1135 trio_to_unsigned_long
1136 TRIO_ARGS3((string, endp, base),
1137 TRIO_CONST char *string,
1142 assert((base >= 2) && (base <= 36));
1144 return strtoul(string, endp, base);
1146 #endif /* !defined(TRIO_MINIMAL) */
1149 #if !defined(TRIO_MINIMAL)
1151 Convert the alphabetic letters in the string to upper-case.
1153 @param target The string to be converted.
1154 @return The number of processed characters (converted or not).
1156 TRIO_STRING_PUBLIC int
1158 TRIO_ARGS1((target),
1163 return trio_span_function(target, target, toupper);
1165 #endif /* !defined(TRIO_MINIMAL) */
1168 /** @} End of StaticStrings */
1171 /*************************************************************************
1172 * Dynamic String Functions
1175 #if defined(TRIO_DOCUMENTATION)
1176 # include "doc/doc_dynamic.h"
1178 /** @addtogroup DynamicStrings
1185 TRIO_STRING_PRIVATE trio_string_t *
1186 TrioStringAlloc(TRIO_NOARGS)
1188 trio_string_t *self;
1190 self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t));
1193 self->content = NULL;
1195 self->allocated = 0;
1204 * The size of the string will be increased by 'delta' characters. If
1205 * 'delta' is zero, the size will be doubled.
1207 TRIO_STRING_PRIVATE BOOLEAN_T
1209 TRIO_ARGS2((self, delta),
1210 trio_string_t *self,
1213 BOOLEAN_T status = FALSE;
1217 new_size = (delta == 0)
1218 ? ( (self->allocated == 0) ? 1 : self->allocated * 2 )
1219 : self->allocated + delta;
1221 new_content = (char *)TRIO_REALLOC(self->content, new_size);
1224 self->content = new_content;
1225 self->allocated = new_size;
1235 * The size of the string will be increased to 'length' plus one characters.
1236 * If 'length' is less than the original size, the original size will be
1237 * used (that is, the size of the string is never decreased).
1239 TRIO_STRING_PRIVATE BOOLEAN_T
1241 TRIO_ARGS2((self, length),
1242 trio_string_t *self,
1245 length++; /* Room for terminating zero */
1246 return (self->allocated < length)
1247 ? TrioStringGrow(self, length - self->allocated)
1252 #if !defined(TRIO_MINIMAL)
1254 Create a new dynamic string.
1256 @param initial_size Initial size of the buffer.
1257 @return Newly allocated dynamic string, or NULL if memory allocation failed.
1259 TRIO_STRING_PUBLIC trio_string_t *
1261 TRIO_ARGS1((initial_size),
1264 trio_string_t *self;
1266 self = TrioStringAlloc();
1269 if (TrioStringGrow(self,
1270 (size_t)((initial_size > 0) ? initial_size : 1)))
1272 self->content[0] = (char)0;
1273 self->allocated = initial_size;
1277 trio_string_destroy(self);
1283 #endif /* !defined(TRIO_MINIMAL) */
1287 Deallocate the dynamic string and its contents.
1289 @param self Dynamic string
1291 TRIO_STRING_PUBLIC void
1294 trio_string_t *self)
1300 trio_destroy(self->content);
1306 #if !defined(TRIO_MINIMAL)
1308 Get a pointer to the content.
1310 @param self Dynamic string.
1311 @param offset Offset into content.
1312 @return Pointer to the content.
1314 @p Offset can be zero, positive, or negative. If @p offset is zero,
1315 then the start of the content will be returned. If @p offset is positive,
1316 then a pointer to @p offset number of characters from the beginning of the
1317 content is returned. If @p offset is negative, then a pointer to @p offset
1318 number of characters from the ending of the string, starting at the
1319 terminating zero, is returned.
1321 TRIO_STRING_PUBLIC char *
1323 TRIO_ARGS2((self, offset),
1324 trio_string_t *self,
1327 char *result = NULL;
1331 if (self->content != NULL)
1333 if (self->length == 0)
1335 (void)trio_string_length(self);
1339 if (offset > (int)self->length)
1341 offset = self->length;
1346 offset += self->length + 1;
1352 result = &(self->content[offset]);
1356 #endif /* !defined(TRIO_MINIMAL) */
1360 Extract the content.
1362 @param self Dynamic String
1363 @return Content of dynamic string.
1365 The content is removed from the dynamic string. This enables destruction
1366 of the dynamic string without deallocation of the content.
1368 TRIO_STRING_PUBLIC char *
1371 trio_string_t *self)
1377 result = self->content;
1378 /* FIXME: Allocate new empty buffer? */
1379 self->content = NULL;
1380 self->length = self->allocated = 0;
1385 #if !defined(TRIO_MINIMAL)
1387 Set the content of the dynamic string.
1389 @param self Dynamic String
1390 @param buffer The new content.
1392 Sets the content of the dynamic string to a copy @p buffer.
1393 An existing content will be deallocated first, if necessary.
1396 This function will make a copy of @p buffer.
1397 You are responsible for deallocating @p buffer yourself.
1399 TRIO_STRING_PUBLIC void
1401 TRIO_ARGS2((self, buffer),
1402 trio_string_t *self,
1407 trio_destroy(self->content);
1408 self->content = trio_duplicate(buffer);
1410 #endif /* !defined(TRIO_MINIMAL) */
1416 TRIO_STRING_PUBLIC int
1419 trio_string_t *self)
1423 return self->allocated;
1428 * trio_string_terminate
1430 TRIO_STRING_PUBLIC void
1431 trio_string_terminate
1433 trio_string_t *self)
1435 trio_xstring_append_char(self, 0);
1439 #if !defined(TRIO_MINIMAL)
1441 Append the second string to the first.
1443 @param self Dynamic string to be modified.
1444 @param other Dynamic string to copy from.
1445 @return Boolean value indicating success or failure.
1447 TRIO_STRING_PUBLIC int
1449 TRIO_ARGS2((self, other),
1450 trio_string_t *self,
1451 trio_string_t *other)
1458 length = self->length + other->length;
1459 if (!TrioStringGrowTo(self, length))
1461 trio_copy(&self->content[self->length], other->content);
1462 self->length = length;
1468 #endif /* !defined(TRIO_MINIMAL) */
1471 #if !defined(TRIO_MINIMAL)
1473 * trio_xstring_append
1475 TRIO_STRING_PUBLIC int
1477 TRIO_ARGS2((self, other),
1478 trio_string_t *self,
1479 TRIO_CONST char *other)
1486 length = self->length + trio_length(other);
1487 if (!TrioStringGrowTo(self, length))
1489 trio_copy(&self->content[self->length], other);
1490 self->length = length;
1496 #endif /* !defined(TRIO_MINIMAL) */
1500 * trio_xstring_append_char
1502 TRIO_STRING_PUBLIC int
1503 trio_xstring_append_char
1504 TRIO_ARGS2((self, character),
1505 trio_string_t *self,
1510 if ((int)self->length >= trio_string_size(self))
1512 if (!TrioStringGrow(self, 0))
1515 self->content[self->length] = character;
1524 #if !defined(TRIO_MINIMAL)
1526 Search for the first occurrence of second parameter in the first.
1528 @param self Dynamic string to be modified.
1529 @param other Dynamic string to copy from.
1530 @return Boolean value indicating success or failure.
1532 TRIO_STRING_PUBLIC int
1533 trio_string_contains
1534 TRIO_ARGS2((self, other),
1535 trio_string_t *self,
1536 trio_string_t *other)
1541 return trio_contains(self->content, other->content);
1543 #endif /* !defined(TRIO_MINIMAL) */
1546 #if !defined(TRIO_MINIMAL)
1548 * trio_xstring_contains
1550 TRIO_STRING_PUBLIC int
1551 trio_xstring_contains
1552 TRIO_ARGS2((self, other),
1553 trio_string_t *self,
1554 TRIO_CONST char *other)
1559 return trio_contains(self->content, other);
1561 #endif /* !defined(TRIO_MINIMAL) */
1564 #if !defined(TRIO_MINIMAL)
1568 TRIO_STRING_PUBLIC int
1570 TRIO_ARGS2((self, other),
1571 trio_string_t *self,
1572 trio_string_t *other)
1578 return trio_string_append(self, other);
1580 #endif /* !defined(TRIO_MINIMAL) */
1583 #if !defined(TRIO_MINIMAL)
1587 TRIO_STRING_PUBLIC int
1589 TRIO_ARGS2((self, other),
1590 trio_string_t *self,
1591 TRIO_CONST char *other)
1597 return trio_xstring_append(self, other);
1599 #endif /* !defined(TRIO_MINIMAL) */
1602 #if !defined(TRIO_MINIMAL)
1604 * trio_string_duplicate
1606 TRIO_STRING_PUBLIC trio_string_t *
1607 trio_string_duplicate
1609 trio_string_t *other)
1611 trio_string_t *self;
1615 self = TrioStringAlloc();
1618 self->content = TrioDuplicateMax(other->content, other->length);
1621 self->length = other->length;
1622 self->allocated = self->length + 1;
1626 self->length = self->allocated = 0;
1631 #endif /* !defined(TRIO_MINIMAL) */
1635 * trio_xstring_duplicate
1637 TRIO_STRING_PUBLIC trio_string_t *
1638 trio_xstring_duplicate
1640 TRIO_CONST char *other)
1642 trio_string_t *self;
1646 self = TrioStringAlloc();
1649 self->content = TrioDuplicateMax(other, trio_length(other));
1652 self->length = trio_length(self->content);
1653 self->allocated = self->length + 1;
1657 self->length = self->allocated = 0;
1664 #if !defined(TRIO_MINIMAL)
1668 TRIO_STRING_PUBLIC int
1670 TRIO_ARGS2((self, other),
1671 trio_string_t *self,
1672 trio_string_t *other)
1677 return trio_equal(self->content, other->content);
1679 #endif /* !defined(TRIO_MINIMAL) */
1682 #if !defined(TRIO_MINIMAL)
1684 * trio_xstring_equal
1686 TRIO_STRING_PUBLIC int
1688 TRIO_ARGS2((self, other),
1689 trio_string_t *self,
1690 TRIO_CONST char *other)
1695 return trio_equal(self->content, other);
1697 #endif /* !defined(TRIO_MINIMAL) */
1700 #if !defined(TRIO_MINIMAL)
1702 * trio_string_equal_max
1704 TRIO_STRING_PUBLIC int
1705 trio_string_equal_max
1706 TRIO_ARGS3((self, max, other),
1707 trio_string_t *self,
1709 trio_string_t *other)
1714 return trio_equal_max(self->content, max, other->content);
1716 #endif /* !defined(TRIO_MINIMAL) */
1719 #if !defined(TRIO_MINIMAL)
1721 * trio_xstring_equal_max
1723 TRIO_STRING_PUBLIC int
1724 trio_xstring_equal_max
1725 TRIO_ARGS3((self, max, other),
1726 trio_string_t *self,
1728 TRIO_CONST char *other)
1733 return trio_equal_max(self->content, max, other);
1735 #endif /* !defined(TRIO_MINIMAL) */
1738 #if !defined(TRIO_MINIMAL)
1740 * trio_string_equal_case
1742 TRIO_STRING_PUBLIC int
1743 trio_string_equal_case
1744 TRIO_ARGS2((self, other),
1745 trio_string_t *self,
1746 trio_string_t *other)
1751 return trio_equal_case(self->content, other->content);
1753 #endif /* !defined(TRIO_MINIMAL) */
1756 #if !defined(TRIO_MINIMAL)
1758 * trio_xstring_equal_case
1760 TRIO_STRING_PUBLIC int
1761 trio_xstring_equal_case
1762 TRIO_ARGS2((self, other),
1763 trio_string_t *self,
1764 TRIO_CONST char *other)
1769 return trio_equal_case(self->content, other);
1771 #endif /* !defined(TRIO_MINIMAL) */
1774 #if !defined(TRIO_MINIMAL)
1776 * trio_string_equal_case_max
1778 TRIO_STRING_PUBLIC int
1779 trio_string_equal_case_max
1780 TRIO_ARGS3((self, max, other),
1781 trio_string_t *self,
1783 trio_string_t *other)
1788 return trio_equal_case_max(self->content, max, other->content);
1790 #endif /* !defined(TRIO_MINIMAL) */
1793 #if !defined(TRIO_MINIMAL)
1795 * trio_xstring_equal_case_max
1797 TRIO_STRING_PUBLIC int
1798 trio_xstring_equal_case_max
1799 TRIO_ARGS3((self, max, other),
1800 trio_string_t *self,
1802 TRIO_CONST char *other)
1807 return trio_equal_case_max(self->content, max, other);
1809 #endif /* !defined(TRIO_MINIMAL) */
1812 #if !defined(TRIO_MINIMAL)
1814 * trio_string_format_data_max
1816 TRIO_STRING_PUBLIC size_t
1817 trio_string_format_date_max
1818 TRIO_ARGS4((self, max, format, datetime),
1819 trio_string_t *self,
1821 TRIO_CONST char *format,
1822 TRIO_CONST struct tm *datetime)
1826 return trio_format_date_max(self->content, max, format, datetime);
1828 #endif /* !defined(TRIO_MINIMAL) */
1831 #if !defined(TRIO_MINIMAL)
1835 TRIO_STRING_PUBLIC char *
1837 TRIO_ARGS2((self, character),
1838 trio_string_t *self,
1843 return trio_index(self->content, character);
1845 #endif /* !defined(TRIO_MINIMAL) */
1848 #if !defined(TRIO_MINIMAL)
1850 * trio_string_index_last
1852 TRIO_STRING_PUBLIC char *
1853 trio_string_index_last
1854 TRIO_ARGS2((self, character),
1855 trio_string_t *self,
1860 return trio_index_last(self->content, character);
1862 #endif /* !defined(TRIO_MINIMAL) */
1865 #if !defined(TRIO_MINIMAL)
1867 * trio_string_length
1869 TRIO_STRING_PUBLIC int
1872 trio_string_t *self)
1876 if (self->length == 0)
1878 self->length = trio_length(self->content);
1880 return self->length;
1882 #endif /* !defined(TRIO_MINIMAL) */
1885 #if !defined(TRIO_MINIMAL)
1889 TRIO_STRING_PUBLIC int
1892 trio_string_t *self)
1896 return trio_lower(self->content);
1898 #endif /* !defined(TRIO_MINIMAL) */
1901 #if !defined(TRIO_MINIMAL)
1905 TRIO_STRING_PUBLIC int
1907 TRIO_ARGS2((self, other),
1908 trio_string_t *self,
1909 trio_string_t *other)
1914 return trio_match(self->content, other->content);
1916 #endif /* !defined(TRIO_MINIMAL) */
1919 #if !defined(TRIO_MINIMAL)
1921 * trio_xstring_match
1923 TRIO_STRING_PUBLIC int
1925 TRIO_ARGS2((self, other),
1926 trio_string_t *self,
1927 TRIO_CONST char *other)
1932 return trio_match(self->content, other);
1934 #endif /* !defined(TRIO_MINIMAL) */
1937 #if !defined(TRIO_MINIMAL)
1939 * trio_string_match_case
1941 TRIO_STRING_PUBLIC int
1942 trio_string_match_case
1943 TRIO_ARGS2((self, other),
1944 trio_string_t *self,
1945 trio_string_t *other)
1950 return trio_match_case(self->content, other->content);
1952 #endif /* !defined(TRIO_MINIMAL) */
1955 #if !defined(TRIO_MINIMAL)
1957 * trio_xstring_match_case
1959 TRIO_STRING_PUBLIC int
1960 trio_xstring_match_case
1961 TRIO_ARGS2((self, other),
1962 trio_string_t *self,
1963 TRIO_CONST char *other)
1968 return trio_match_case(self->content, other);
1970 #endif /* !defined(TRIO_MINIMAL) */
1973 #if !defined(TRIO_MINIMAL)
1975 * trio_string_substring
1977 TRIO_STRING_PUBLIC char *
1978 trio_string_substring
1979 TRIO_ARGS2((self, other),
1980 trio_string_t *self,
1981 trio_string_t *other)
1986 return trio_substring(self->content, other->content);
1988 #endif /* !defined(TRIO_MINIMAL) */
1991 #if !defined(TRIO_MINIMAL)
1993 * trio_xstring_substring
1995 TRIO_STRING_PUBLIC char *
1996 trio_xstring_substring
1997 TRIO_ARGS2((self, other),
1998 trio_string_t *self,
1999 TRIO_CONST char *other)
2004 return trio_substring(self->content, other);
2006 #endif /* !defined(TRIO_MINIMAL) */
2009 #if !defined(TRIO_MINIMAL)
2013 TRIO_STRING_PUBLIC int
2016 trio_string_t *self)
2020 return trio_upper(self->content);
2022 #endif /* !defined(TRIO_MINIMAL) */
2024 /** @} End of DynamicStrings */