1 /*************************************************************************
3 * $Id: triostr.c,v 1.34 2008/11/09 12:17:39 breese Exp $
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 /*************************************************************************
22 #if defined(HAVE_CONFIG_H)
31 #if defined(TRIO_FUNC_TO_LONG_DOUBLE)
38 /*************************************************************************
42 #if !defined(TRIO_PUBLIC_STRING)
43 # define TRIO_PUBLIC_STRING TRIO_PUBLIC
45 #if !defined(TRIO_PRIVATE_STRING)
46 # define TRIO_PRIVATE_STRING TRIO_PRIVATE
53 # define NIL ((char)0)
56 # define FALSE (1 == 0)
57 # define TRUE (! FALSE)
59 #if !defined(BOOLEAN_T)
60 # define BOOLEAN_T int
64 # if defined(PREDEF_STANDARD_C99)
65 # if defined(TRIO_COMPILER_DECC)
66 # if (TRIO_COMPILER_DECC - 0 > 80000000)
68 * The OSF/1 runtime that comes with the DECC compiler does not support
69 * hexfloats conversion.
79 # if defined(TRIO_COMPILER_VISUALC)
85 #if defined(TRIO_PLATFORM_UNIX)
86 # if defined(PREDEF_STANDARD_UNIX95)
87 # define USE_STRCASECMP
88 # define USE_STRNCASECMP
90 # if defined(TRIO_PLATFORM_SUNOS)
91 # define USE_SYS_ERRLIST
95 # if defined(TRIO_PLATFORM_QNX)
96 # define strcasecmp(x,y) stricmp(x,y)
97 # define strncasecmp(x,y,n) strnicmp(x,y,n)
101 #if defined(TRIO_PLATFORM_WIN32)
102 # define USE_STRCASECMP
103 # if defined(TRIO_PLATFORM_WINCE)
104 # define strcasecmp(x,y) _stricmp(x,y)
106 # define strcasecmp(x,y) strcmpi(x,y)
110 #if !defined(HAVE_CONFIG_H)
111 # if !(defined(TRIO_PLATFORM_SUNOS))
112 # define HAVE_TOLOWER
113 # define HAVE_TOUPPER
117 #if defined(USE_MATH)
118 # if !defined(HAVE_POWL)
119 # if defined(PREDEF_STANDARD_C99) \
120 || defined(PREDEF_STANDARD_UNIX03)
123 # if defined(TRIO_COMPILER_VISUALC)
132 #if defined(HAVE_POWL)
133 # define trio_powl(x,y) powl((x),(y))
135 # define trio_powl(x,y) pow((double)(x),(double)(y))
138 #if defined(TRIO_FUNC_TO_UPPER) \
139 || (defined(TRIO_FUNC_EQUAL) && !defined(USE_STRCASECMP)) \
140 || (defined(TRIO_FUNC_EQUAL_MAX) && !defined(USE_STRNCASECMP)) \
141 || defined(TRIO_FUNC_MATCH) \
142 || defined(TRIO_FUNC_TO_LONG_DOUBLE) \
143 || defined(TRIO_FUNC_UPPER)
144 # define TRIO_FUNC_INTERNAL_TO_UPPER
147 /*************************************************************************
151 struct _trio_string_t
158 /*************************************************************************
162 #if !defined(TRIO_EMBED_STRING)
163 static TRIO_CONST char rcsid[] = "@(#)$Id: triostr.c,v 1.34 2008/11/09 12:17:39 breese Exp $";
166 /*************************************************************************
167 * Static String Functions
170 #if defined(TRIO_DOCUMENTATION)
171 # include "doc/doc_static.h"
173 /** @addtogroup StaticStrings
178 * internal_duplicate_max
180 #if defined(TRIO_FUNC_DUPLICATE) \
181 || defined(TRIO_FUNC_DUPLICATE_MAX) \
182 || defined(TRIO_FUNC_STRING_DUPLICATE) \
183 || defined(TRIO_FUNC_XSTRING_DUPLICATE)
185 TRIO_PRIVATE_STRING char *
186 internal_duplicate_max
187 TRIO_ARGS2((source, size),
188 TRIO_CONST char *source,
195 /* Make room for string plus a terminating zero */
197 target = trio_create(size);
200 trio_copy_max(target, size, source);
208 * internal_string_alloc
210 #if defined(TRIO_FUNC_STRING_CREATE) \
211 || defined(TRIO_FUNC_STRING_DUPLICATE) \
212 || defined(TRIO_FUNC_XSTRING_DUPLICATE)
214 TRIO_PRIVATE_STRING trio_string_t *
215 internal_string_alloc(TRIO_NOARGS)
219 self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t));
222 self->content = NULL;
232 * internal_string_grow
234 * The size of the string will be increased by 'delta' characters. If
235 * 'delta' is zero, the size will be doubled.
237 #if defined(TRIO_FUNC_STRING_CREATE) \
238 || defined(TRIO_FUNC_STRING_APPEND) \
239 || defined(TRIO_FUNC_XSTRING_APPEND) \
240 || defined(TRIO_FUNC_XSTRING_APPEND_CHAR)
242 TRIO_PRIVATE_STRING BOOLEAN_T
244 TRIO_ARGS2((self, delta),
248 BOOLEAN_T status = FALSE;
252 new_size = (delta == 0)
253 ? ( (self->allocated == 0) ? 1 : self->allocated * 2 )
254 : self->allocated + delta;
256 new_content = (char *)TRIO_REALLOC(self->content, new_size);
259 self->content = new_content;
260 self->allocated = new_size;
269 * internal_string_grow_to
271 * The size of the string will be increased to 'length' plus one characters.
272 * If 'length' is less than the original size, the original size will be
273 * used (that is, the size of the string is never decreased).
275 #if defined(TRIO_FUNC_STRING_APPEND) \
276 || defined(TRIO_FUNC_XSTRING_APPEND)
278 TRIO_PRIVATE_STRING BOOLEAN_T
279 internal_string_grow_to
280 TRIO_ARGS2((self, length),
284 length++; /* Room for terminating zero */
285 return (self->allocated < length)
286 ? internal_string_grow(self, length - self->allocated)
292 #if defined(TRIO_FUNC_INTERNAL_TO_UPPER)
294 TRIO_PRIVATE_STRING TRIO_INLINE int
299 # if defined(HAVE_TOUPPER)
301 return toupper(source);
305 /* Does not handle locales or non-contiguous alphabetic characters */
306 return ((source >= (int)'a') && (source <= (int)'z'))
319 @param size Size of new string.
320 @return Pointer to string, or NULL if allocation failed.
322 #if defined(TRIO_FUNC_CREATE)
324 TRIO_PUBLIC_STRING char *
329 return (char *)TRIO_MALLOC(size);
337 @param string String to be freed.
339 #if defined(TRIO_FUNC_DESTROY)
341 TRIO_PUBLIC_STRING void
355 Count the number of characters in a string.
357 @param string String to measure.
358 @return Number of characters in @p string.
360 #if defined(TRIO_FUNC_LENGTH)
362 TRIO_PUBLIC_STRING size_t
365 TRIO_CONST char *string)
367 return strlen(string);
373 Count at most @p max characters in a string.
375 @param string String to measure.
376 @param max Maximum number of characters to count.
377 @return The maximum value of @p max and number of characters in @p string.
379 #if defined(TRIO_FUNC_LENGTH)
381 TRIO_PUBLIC_STRING size_t
383 TRIO_ARGS2((string, max),
384 TRIO_CONST char *string,
389 for (i = 0; i < max; ++i)
400 Append @p source at the end of @p target.
402 @param target Target string.
403 @param source Source string.
404 @return Boolean value indicating success or failure.
406 @pre @p target must point to a memory chunk with sufficient room to
407 contain the @p target string and @p source string.
408 @pre No boundary checking is performed, so insufficient memory will
409 result in a buffer overrun.
410 @post @p target will be zero terminated.
412 #if defined(TRIO_FUNC_APPEND)
414 TRIO_PUBLIC_STRING int
416 TRIO_ARGS2((target, source),
418 TRIO_CONST char *source)
423 return (strcat(target, source) != NULL);
429 Append at most @p max characters from @p source to @p target.
431 @param target Target string.
432 @param max Maximum number of characters to append.
433 @param source Source string.
434 @return Boolean value indicating success or failure.
436 @pre @p target must point to a memory chuck with sufficient room to
437 contain the @p target string and the @p source string (at most @p max
439 @pre No boundary checking is performed, so insufficient memory will
440 result in a buffer overrun.
441 @post @p target will be zero terminated.
443 #if defined(TRIO_FUNC_APPEND_MAX)
445 TRIO_PUBLIC_STRING int
447 TRIO_ARGS3((target, max, source),
450 TRIO_CONST char *source)
457 length = trio_length(target);
461 strncat(target, source, max - length - 1);
469 Determine if a string contains a substring.
471 @param string String to be searched.
472 @param substring String to be found.
473 @return Boolean value indicating success or failure.
475 #if defined(TRIO_FUNC_CONTAINS)
477 TRIO_PUBLIC_STRING int
479 TRIO_ARGS2((string, substring),
480 TRIO_CONST char *string,
481 TRIO_CONST char *substring)
486 return (0 != strstr(string, substring));
492 Copy @p source to @p target.
494 @param target Target string.
495 @param source Source string.
496 @return Boolean value indicating success or failure.
498 @pre @p target must point to a memory chunk with sufficient room to
499 contain the @p source string.
500 @pre No boundary checking is performed, so insufficient memory will
501 result in a buffer overrun.
502 @post @p target will be zero terminated.
504 #if defined(TRIO_FUNC_COPY)
506 TRIO_PUBLIC_STRING int
508 TRIO_ARGS2((target, source),
510 TRIO_CONST char *source)
515 (void)strcpy(target, source);
522 Copy at most @p max characters from @p source to @p target.
524 @param target Target string.
525 @param max Maximum number of characters to append.
526 @param source Source string.
527 @return Boolean value indicating success or failure.
529 @pre @p target must point to a memory chunk with sufficient room to
530 contain the @p source string (at most @p max characters).
531 @pre No boundary checking is performed, so insufficient memory will
532 result in a buffer overrun.
533 @post @p target will be zero terminated.
535 #if defined(TRIO_FUNC_COPY_MAX)
537 TRIO_PUBLIC_STRING int
539 TRIO_ARGS3((target, max, source),
542 TRIO_CONST char *source)
546 assert(max > 0); /* Includes != 0 */
548 (void)strncpy(target, source, max - 1);
549 target[max - 1] = (char)0;
558 @param source Source string.
559 @return A copy of the @p source string.
561 @post @p target will be zero terminated.
563 #if defined(TRIO_FUNC_DUPLICATE)
565 TRIO_PUBLIC_STRING char *
568 TRIO_CONST char *source)
570 return internal_duplicate_max(source, trio_length(source));
576 Duplicate at most @p max characters of @p source.
578 @param source Source string.
579 @param max Maximum number of characters to duplicate.
580 @return A copy of the @p source string.
582 @post @p target will be zero terminated.
584 #if defined(TRIO_FUNC_DUPLICATE_MAX)
586 TRIO_PUBLIC_STRING char *
588 TRIO_ARGS2((source, max),
589 TRIO_CONST char *source,
597 length = trio_length(source);
602 return internal_duplicate_max(source, length);
608 Compare if two strings are equal.
610 @param first First string.
611 @param second Second string.
612 @return Boolean indicating whether the two strings are equal or not.
614 Case-insensitive comparison.
616 #if defined(TRIO_FUNC_EQUAL)
618 TRIO_PUBLIC_STRING int
620 TRIO_ARGS2((first, second),
621 TRIO_CONST char *first,
622 TRIO_CONST char *second)
627 if ((first != NULL) && (second != NULL))
629 # if defined(USE_STRCASECMP)
630 return (0 == strcasecmp(first, second));
632 while ((*first != NIL) && (*second != NIL))
634 if (internal_to_upper(*first) != internal_to_upper(*second))
641 return ((*first == NIL) && (*second == NIL));
650 Compare if two strings are equal.
652 @param first First string.
653 @param second Second string.
654 @return Boolean indicating whether the two strings are equal or not.
656 Case-sensitive comparison.
658 #if defined(TRIO_FUNC_EQUAL_CASE)
660 TRIO_PUBLIC_STRING int
662 TRIO_ARGS2((first, second),
663 TRIO_CONST char *first,
664 TRIO_CONST char *second)
669 if ((first != NULL) && (second != NULL))
671 return (0 == strcmp(first, second));
679 Compare if two strings up until the first @p max characters are equal.
681 @param first First string.
682 @param max Maximum number of characters to compare.
683 @param second Second string.
684 @return Boolean indicating whether the two strings are equal or not.
686 Case-sensitive comparison.
688 #if defined(TRIO_FUNC_EQUAL_CASE_MAX)
690 TRIO_PUBLIC_STRING int
692 TRIO_ARGS3((first, max, second),
693 TRIO_CONST char *first,
695 TRIO_CONST char *second)
700 if ((first != NULL) && (second != NULL))
702 return (0 == strncmp(first, second, max));
710 Compare if two strings are equal.
712 @param first First string.
713 @param second Second string.
714 @return Boolean indicating whether the two strings are equal or not.
716 Collating characters are considered equal.
718 #if defined(TRIO_FUNC_EQUAL_LOCALE)
720 TRIO_PUBLIC_STRING int
722 TRIO_ARGS2((first, second),
723 TRIO_CONST char *first,
724 TRIO_CONST char *second)
729 # if defined(LC_COLLATE)
730 return (strcoll(first, second) == 0);
732 return trio_equal(first, second);
739 Compare if two strings up until the first @p max characters are equal.
741 @param first First string.
742 @param max Maximum number of characters to compare.
743 @param second Second string.
744 @return Boolean indicating whether the two strings are equal or not.
746 Case-insensitive comparison.
748 #if defined(TRIO_FUNC_EQUAL_MAX)
750 TRIO_PUBLIC_STRING int
752 TRIO_ARGS3((first, max, second),
753 TRIO_CONST char *first,
755 TRIO_CONST char *second)
760 if ((first != NULL) && (second != NULL))
762 # if defined(USE_STRNCASECMP)
763 return (0 == strncasecmp(first, second, max));
765 /* Not adequately tested yet */
767 while ((*first != NIL) && (*second != NIL) && (cnt <= max))
769 if (internal_to_upper(*first) != internal_to_upper(*second))
777 return ((cnt == max) || ((*first == NIL) && (*second == NIL)));
786 Provide a textual description of an error code (errno).
788 @param error_number Error number.
789 @return Textual description of @p error_number.
791 #if defined(TRIO_FUNC_ERROR)
793 TRIO_PUBLIC_STRING TRIO_CONST char *
795 TRIO_ARGS1((error_number),
798 # if defined(USE_STRERROR)
800 return strerror(error_number);
803 # if defined(USE_SYS_ERRLIST)
805 extern char *sys_errlist[];
808 return ((error_number < 0) || (error_number >= sys_nerr))
810 : sys_errlist[error_number];
823 Format the date/time according to @p format.
825 @param target Target string.
826 @param max Maximum number of characters to format.
827 @param format Formatting string.
828 @param datetime Date/time structure.
829 @return Number of formatted characters.
831 The formatting string accepts the same specifiers as the standard C
834 #if defined(TRIO_FUNC_FORMAT_DATE_MAX)
836 TRIO_PUBLIC_STRING size_t
838 TRIO_ARGS4((target, max, format, datetime),
841 TRIO_CONST char *format,
842 TRIO_CONST struct tm *datetime)
849 return strftime(target, max, format, datetime);
855 Calculate a hash value for a string.
857 @param string String to be calculated on.
858 @param type Hash function.
859 @return Calculated hash value.
861 @p type can be one of the following
862 @li @c TRIO_HASH_PLAIN Plain hash function.
864 #if defined(TRIO_FUNC_HASH)
866 TRIO_PUBLIC_STRING unsigned long
868 TRIO_ARGS2((string, type),
869 TRIO_CONST char *string,
872 unsigned long value = 0L;
879 case TRIO_HASH_PLAIN:
880 while ( (ch = *string++) != NIL )
883 value += (unsigned long)ch;
896 Find first occurrence of a character in a string.
898 @param string String to be searched.
899 @param character Character to be found.
900 @return A pointer to the found character, or NULL if character was not found.
902 #if defined(TRIO_FUNC_INDEX)
904 TRIO_PUBLIC_STRING char *
906 TRIO_ARGS2((string, character),
907 TRIO_CONST char *string,
912 return strchr(string, character);
918 Find last occurrence of a character in a string.
920 @param string String to be searched.
921 @param character Character to be found.
922 @return A pointer to the found character, or NULL if character was not found.
924 #if defined(TRIO_FUNC_INDEX_LAST)
926 TRIO_PUBLIC_STRING char *
928 TRIO_ARGS2((string, character),
929 TRIO_CONST char *string,
934 return strchr(string, character);
940 Convert the alphabetic letters in the string to lower-case.
942 @param target String to be converted.
943 @return Number of processed characters (converted or not).
945 #if defined(TRIO_FUNC_LOWER)
947 TRIO_PUBLIC_STRING int
954 return trio_span_function(target, target, trio_to_lower);
960 Compare two strings using wildcards.
962 @param string String to be searched.
963 @param pattern Pattern, including wildcards, to search for.
964 @return Boolean value indicating success or failure.
966 Case-insensitive comparison.
968 The following wildcards can be used
969 @li @c * Match any number of characters.
970 @li @c ? Match a single character.
972 #if defined(TRIO_FUNC_MATCH)
974 TRIO_PUBLIC_STRING int
976 TRIO_ARGS2((string, pattern),
977 TRIO_CONST char *string,
978 TRIO_CONST char *pattern)
983 for (; ('*' != *pattern); ++pattern, ++string)
987 return (NIL == *pattern);
989 if ((internal_to_upper((int)*string) != internal_to_upper((int)*pattern))
990 && ('?' != *pattern))
995 /* two-line patch to prevent *too* much recursiveness: */
996 while ('*' == pattern[1])
1001 if ( trio_match(string, &pattern[1]) )
1014 Compare two strings using wildcards.
1016 @param string String to be searched.
1017 @param pattern Pattern, including wildcards, to search for.
1018 @return Boolean value indicating success or failure.
1020 Case-sensitive comparison.
1022 The following wildcards can be used
1023 @li @c * Match any number of characters.
1024 @li @c ? Match a single character.
1026 #if defined(TRIO_FUNC_MATCH_CASE)
1028 TRIO_PUBLIC_STRING int
1030 TRIO_ARGS2((string, pattern),
1031 TRIO_CONST char *string,
1032 TRIO_CONST char *pattern)
1037 for (; ('*' != *pattern); ++pattern, ++string)
1041 return (NIL == *pattern);
1043 if ((*string != *pattern)
1044 && ('?' != *pattern))
1049 /* two-line patch to prevent *too* much recursiveness: */
1050 while ('*' == pattern[1])
1055 if ( trio_match_case(string, &pattern[1]) )
1068 Execute a function on each character in string.
1070 @param target Target string.
1071 @param source Source string.
1072 @param Function Function to be executed.
1073 @return Number of processed characters.
1075 #if defined(TRIO_FUNC_SPAN_FUNCTION)
1077 TRIO_PUBLIC_STRING size_t
1079 TRIO_ARGS3((target, source, Function),
1081 TRIO_CONST char *source,
1082 int (*Function) TRIO_PROTO((int)))
1090 while (*source != NIL)
1092 *target++ = Function(*source++);
1101 Search for a substring in a string.
1103 @param string String to be searched.
1104 @param substring String to be found.
1105 @return Pointer to first occurrence of @p substring in @p string, or NULL
1106 if no match was found.
1108 #if defined(TRIO_FUNC_SUBSTRING)
1110 TRIO_PUBLIC_STRING char *
1112 TRIO_ARGS2((string, substring),
1113 TRIO_CONST char *string,
1114 TRIO_CONST char *substring)
1119 return strstr(string, substring);
1125 Search for a substring in the first @p max characters of a string.
1127 @param string String to be searched.
1128 @param max Maximum characters to be searched.
1129 @param substring String to be found.
1130 @return Pointer to first occurrence of @p substring in @p string, or NULL
1131 if no match was found.
1133 #if defined(TRIO_FUNC_SUBSTRING_MAX)
1135 TRIO_PUBLIC_STRING char *
1137 TRIO_ARGS3((string, max, substring),
1138 TRIO_CONST char *string,
1140 TRIO_CONST char *substring)
1144 char *result = NULL;
1149 size = trio_length(substring);
1152 for (count = 0; count <= max - size; count++)
1154 if (trio_equal_max(substring, size, &string[count]))
1156 result = (char *)&string[count];
1169 @param string String to be tokenized.
1170 @param delimiters String containing list of delimiting characters.
1171 @return Start of new token.
1173 @warning @p string will be destroyed.
1175 #if defined(TRIO_FUNC_TOKENIZE)
1177 TRIO_PUBLIC_STRING char *
1179 TRIO_ARGS2((string, delimiters),
1181 TRIO_CONST char *delimiters)
1185 return strtok(string, delimiters);
1191 Convert string to floating-point number.
1193 @param source String to be converted.
1194 @param endp Pointer to end of the converted string.
1195 @return A floating-point number.
1197 The following Extended Backus-Naur form is used
1199 double ::= [ <sign> ]
1201 <number> <decimal_point> <number> |
1202 <decimal_point> <number> )
1203 [ <exponential> [ <sign> ] <number> ]
1204 number ::= 1*( <digit> )
1205 digit ::= ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )
1206 exponential ::= ( 'e' | 'E' )
1207 sign ::= ( '-' | '+' )
1208 decimal_point ::= '.'
1211 #if defined(TRIO_FUNC_TO_LONG_DOUBLE)
1213 /* FIXME: Add EBNF for hex-floats */
1214 TRIO_PUBLIC_STRING trio_long_double_t
1216 TRIO_ARGS2((source, endp),
1217 TRIO_CONST char *source,
1220 # if defined(USE_STRTOLD)
1221 return strtold(source, endp);
1223 int isNegative = FALSE;
1224 int isExponentNegative = FALSE;
1225 trio_long_double_t integer = 0.0;
1226 trio_long_double_t fraction = 0.0;
1227 unsigned long exponent = 0;
1228 trio_long_double_t base;
1229 trio_long_double_t fracdiv = 1.0;
1230 trio_long_double_t value = 0.0;
1232 /* First try hex-floats */
1233 if ((source[0] == '0') && ((source[1] == 'x') || (source[1] == 'X')))
1237 while (isxdigit((int)*source))
1240 integer += (isdigit((int)*source)
1242 : 10 + (internal_to_upper((int)*source) - 'A'));
1248 while (isxdigit((int)*source))
1251 fraction += fracdiv * (isdigit((int)*source)
1253 : 10 + (internal_to_upper((int)*source) - 'A'));
1256 if ((*source == 'p') || (*source == 'P'))
1259 if ((*source == '+') || (*source == '-'))
1261 isExponentNegative = (*source == '-');
1264 while (isdigit((int)*source))
1267 exponent += (*source - '0');
1272 /* For later use with exponent */
1275 else /* Then try normal decimal floats */
1278 isNegative = (*source == '-');
1280 if ((*source == '+') || (*source == '-'))
1284 while (isdigit((int)*source))
1287 integer += (*source - '0');
1293 source++; /* skip decimal point */
1294 while (isdigit((int)*source))
1297 fraction += (*source - '0') * fracdiv;
1301 if ((*source == 'e')
1309 source++; /* Skip exponential indicator */
1310 isExponentNegative = (*source == '-');
1311 if ((*source == '+') || (*source == '-'))
1313 while (isdigit((int)*source))
1315 exponent *= (int)base;
1316 exponent += (*source - '0');
1322 value = integer + fraction;
1325 if (isExponentNegative)
1326 value /= trio_powl(base, (trio_long_double_t)exponent);
1328 value *= trio_powl(base, (trio_long_double_t)exponent);
1334 *endp = (char *)source;
1342 Convert string to floating-point number.
1344 @param source String to be converted.
1345 @param endp Pointer to end of the converted string.
1346 @return A floating-point number.
1348 See @ref trio_to_long_double.
1350 #if defined(TRIO_FUNC_TO_DOUBLE)
1352 TRIO_PUBLIC_STRING double
1354 TRIO_ARGS2((source, endp),
1355 TRIO_CONST char *source,
1358 #if defined(USE_STRTOD)
1359 return strtod(source, endp);
1361 return (double)trio_to_long_double(source, endp);
1368 Convert string to floating-point number.
1370 @param source String to be converted.
1371 @param endp Pointer to end of the converted string.
1372 @return A floating-point number.
1374 See @ref trio_to_long_double.
1376 #if defined(TRIO_FUNC_TO_FLOAT)
1378 TRIO_PUBLIC_STRING float
1380 TRIO_ARGS2((source, endp),
1381 TRIO_CONST char *source,
1384 # if defined(USE_STRTOF)
1385 return strtof(source, endp);
1387 return (float)trio_to_long_double(source, endp);
1394 Convert string to signed integer.
1396 @param string String to be converted.
1397 @param endp Pointer to end of converted string.
1398 @param base Radix number of number.
1400 #if defined(TRIO_FUNC_TO_LONG)
1402 TRIO_PUBLIC_STRING long
1404 TRIO_ARGS3((string, endp, base),
1405 TRIO_CONST char *string,
1410 assert((base >= 2) && (base <= 36));
1412 return strtol(string, endp, base);
1418 Convert one alphabetic letter to lower-case.
1420 @param source The letter to be converted.
1421 @return The converted letter.
1423 #if defined(TRIO_FUNC_TO_LOWER)
1425 TRIO_PUBLIC_STRING int
1427 TRIO_ARGS1((source),
1430 # if defined(HAVE_TOLOWER)
1432 return tolower(source);
1436 /* Does not handle locales or non-contiguous alphabetic characters */
1437 return ((source >= (int)'A') && (source <= (int)'Z'))
1438 ? source - 'A' + 'a'
1447 Convert string to unsigned integer.
1449 @param string String to be converted.
1450 @param endp Pointer to end of converted string.
1451 @param base Radix number of number.
1453 #if defined(TRIO_FUNC_TO_UNSIGNED_LONG)
1455 TRIO_PUBLIC_STRING unsigned long
1456 trio_to_unsigned_long
1457 TRIO_ARGS3((string, endp, base),
1458 TRIO_CONST char *string,
1463 assert((base >= 2) && (base <= 36));
1465 return strtoul(string, endp, base);
1471 Convert one alphabetic letter to upper-case.
1473 @param source The letter to be converted.
1474 @return The converted letter.
1476 #if defined(TRIO_FUNC_TO_UPPER)
1478 TRIO_PUBLIC_STRING int
1480 TRIO_ARGS1((source),
1483 return internal_to_upper(source);
1489 Convert the alphabetic letters in the string to upper-case.
1491 @param target The string to be converted.
1492 @return The number of processed characters (converted or not).
1494 #if defined(TRIO_FUNC_UPPER)
1496 TRIO_PUBLIC_STRING int
1498 TRIO_ARGS1((target),
1503 return trio_span_function(target, target, internal_to_upper);
1508 /** @} End of StaticStrings */
1511 /*************************************************************************
1512 * Dynamic String Functions
1515 #if defined(TRIO_DOCUMENTATION)
1516 # include "doc/doc_dynamic.h"
1518 /** @addtogroup DynamicStrings
1523 Create a new dynamic string.
1525 @param initial_size Initial size of the buffer.
1526 @return Newly allocated dynamic string, or NULL if memory allocation failed.
1528 #if defined(TRIO_FUNC_STRING_CREATE)
1530 TRIO_PUBLIC_STRING trio_string_t *
1532 TRIO_ARGS1((initial_size),
1535 trio_string_t *self;
1537 self = internal_string_alloc();
1540 if (internal_string_grow(self,
1541 (size_t)((initial_size > 0) ? initial_size : 1)))
1543 self->content[0] = (char)0;
1544 self->allocated = initial_size;
1548 trio_string_destroy(self);
1558 Deallocate the dynamic string and its contents.
1560 @param self Dynamic string
1562 #if defined(TRIO_FUNC_STRING_DESTROY)
1564 TRIO_PUBLIC_STRING void
1567 trio_string_t *self)
1573 trio_destroy(self->content);
1581 Get a pointer to the content.
1583 @param self Dynamic string.
1584 @param offset Offset into content.
1585 @return Pointer to the content.
1587 @p Offset can be zero, positive, or negative. If @p offset is zero,
1588 then the start of the content will be returned. If @p offset is positive,
1589 then a pointer to @p offset number of characters from the beginning of the
1590 content is returned. If @p offset is negative, then a pointer to @p offset
1591 number of characters from the ending of the string, starting at the
1592 terminating zero, is returned.
1594 #if defined(TRIO_FUNCT_STRING_GET)
1596 TRIO_PUBLIC_STRING char *
1598 TRIO_ARGS2((self, offset),
1599 trio_string_t *self,
1602 char *result = NULL;
1606 if (self->content != NULL)
1608 if (self->length == 0)
1610 (void)trio_string_length(self);
1614 if (offset > (int)self->length)
1616 offset = self->length;
1621 offset += self->length + 1;
1627 result = &(self->content[offset]);
1635 Extract the content.
1637 @param self Dynamic String
1638 @return Content of dynamic string.
1640 The content is removed from the dynamic string. This enables destruction
1641 of the dynamic string without deallocation of the content.
1643 #if defined(TRIO_FUNC_STRING_EXTRACT)
1645 TRIO_PUBLIC_STRING char *
1648 trio_string_t *self)
1654 result = self->content;
1655 /* FIXME: Allocate new empty buffer? */
1656 self->content = NULL;
1657 self->length = self->allocated = 0;
1664 Set the content of the dynamic string.
1666 @param self Dynamic String
1667 @param buffer The new content.
1669 Sets the content of the dynamic string to a copy @p buffer.
1670 An existing content will be deallocated first, if necessary.
1673 This function will make a copy of @p buffer.
1674 You are responsible for deallocating @p buffer yourself.
1676 #if defined(TRIO_FUNC_XSTRING_SET)
1678 TRIO_PUBLIC_STRING void
1680 TRIO_ARGS2((self, buffer),
1681 trio_string_t *self,
1686 trio_destroy(self->content);
1687 self->content = trio_duplicate(buffer);
1695 #if defined(TRIO_FUNC_STRING_SIZE)
1697 TRIO_PUBLIC_STRING int
1700 trio_string_t *self)
1704 return self->allocated;
1710 * trio_string_terminate
1712 #if defined(TRIO_FUNC_STRING_TERMINATE)
1714 TRIO_PUBLIC_STRING void
1715 trio_string_terminate
1717 trio_string_t *self)
1719 trio_xstring_append_char(self, 0);
1725 Append the second string to the first.
1727 @param self Dynamic string to be modified.
1728 @param other Dynamic string to copy from.
1729 @return Boolean value indicating success or failure.
1731 #if defined(TRIO_FUNC_STRING_APPEND)
1733 TRIO_PUBLIC_STRING int
1735 TRIO_ARGS2((self, other),
1736 trio_string_t *self,
1737 trio_string_t *other)
1744 length = self->length + other->length;
1745 if (!internal_string_grow_to(self, length))
1747 trio_copy(&self->content[self->length], other->content);
1748 self->length = length;
1759 * trio_xstring_append
1761 #if defined(TRIO_FUNC_XSTRING_APPEND)
1763 TRIO_PUBLIC_STRING int
1765 TRIO_ARGS2((self, other),
1766 trio_string_t *self,
1767 TRIO_CONST char *other)
1774 length = self->length + trio_length(other);
1775 if (!internal_string_grow_to(self, length))
1777 trio_copy(&self->content[self->length], other);
1778 self->length = length;
1788 * trio_xstring_append_char
1790 #if defined(TRIO_FUNC_XSTRING_APPEND_CHAR)
1792 TRIO_PUBLIC_STRING int
1793 trio_xstring_append_char
1794 TRIO_ARGS2((self, character),
1795 trio_string_t *self,
1800 if ((int)self->length >= trio_string_size(self))
1802 if (!internal_string_grow(self, 0))
1805 self->content[self->length] = character;
1816 Search for the first occurrence of second parameter in the first.
1818 @param self Dynamic string to be modified.
1819 @param other Dynamic string to copy from.
1820 @return Boolean value indicating success or failure.
1822 #if defined(TRIO_FUNC_STRING_CONTAINS)
1824 TRIO_PUBLIC_STRING int
1825 trio_string_contains
1826 TRIO_ARGS2((self, other),
1827 trio_string_t *self,
1828 trio_string_t *other)
1833 return trio_contains(self->content, other->content);
1839 * trio_xstring_contains
1841 #if defined(TRIO_FUNC_XSTRING_CONTAINS)
1843 TRIO_PUBLIC_STRING int
1844 trio_xstring_contains
1845 TRIO_ARGS2((self, other),
1846 trio_string_t *self,
1847 TRIO_CONST char *other)
1852 return trio_contains(self->content, other);
1860 #if defined(TRIO_FUNC_STRING_COPY)
1862 TRIO_PUBLIC_STRING int
1864 TRIO_ARGS2((self, other),
1865 trio_string_t *self,
1866 trio_string_t *other)
1872 return trio_string_append(self, other);
1881 #if defined(TRIO_FUNC_XSTRING_COPY)
1883 TRIO_PUBLIC_STRING int
1885 TRIO_ARGS2((self, other),
1886 trio_string_t *self,
1887 TRIO_CONST char *other)
1893 return trio_xstring_append(self, other);
1899 * trio_string_duplicate
1901 #if defined(TRIO_FUNC_STRING_DUPLICATE)
1903 TRIO_PUBLIC_STRING trio_string_t *
1904 trio_string_duplicate
1906 trio_string_t *other)
1908 trio_string_t *self;
1912 self = internal_string_alloc();
1915 self->content = internal_duplicate_max(other->content, other->length);
1918 self->length = other->length;
1919 self->allocated = self->length + 1;
1923 self->length = self->allocated = 0;
1932 * trio_xstring_duplicate
1934 #if defined(TRIO_FUNC_XSTRING_DUPLICATE)
1936 TRIO_PUBLIC_STRING trio_string_t *
1937 trio_xstring_duplicate
1939 TRIO_CONST char *other)
1941 trio_string_t *self;
1945 self = internal_string_alloc();
1948 self->content = internal_duplicate_max(other, trio_length(other));
1951 self->length = trio_length(self->content);
1952 self->allocated = self->length + 1;
1956 self->length = self->allocated = 0;
1967 #if defined(TRIO_FUNC_STRING_EQUAL)
1969 TRIO_PUBLIC_STRING int
1971 TRIO_ARGS2((self, other),
1972 trio_string_t *self,
1973 trio_string_t *other)
1978 return trio_equal(self->content, other->content);
1985 * trio_xstring_equal
1987 #if defined(TRIO_FUNC_XSTRING_EQUAL)
1989 TRIO_PUBLIC_STRING int
1991 TRIO_ARGS2((self, other),
1992 trio_string_t *self,
1993 TRIO_CONST char *other)
1998 return trio_equal(self->content, other);
2004 * trio_string_equal_max
2006 #if defined(TRIO_FUNC_STRING_EQUAL_MAX)
2008 TRIO_PUBLIC_STRING int
2009 trio_string_equal_max
2010 TRIO_ARGS3((self, max, other),
2011 trio_string_t *self,
2013 trio_string_t *other)
2018 return trio_equal_max(self->content, max, other->content);
2023 * trio_xstring_equal_max
2025 #if defined(TRIO_FUNC_XSTRING_EQUAL_MAX)
2027 TRIO_PUBLIC_STRING int
2028 trio_xstring_equal_max
2029 TRIO_ARGS3((self, max, other),
2030 trio_string_t *self,
2032 TRIO_CONST char *other)
2037 return trio_equal_max(self->content, max, other);
2043 * trio_string_equal_case
2045 #if defined(TRIO_FUNC_STRING_EQUAL_CASE)
2047 TRIO_PUBLIC_STRING int
2048 trio_string_equal_case
2049 TRIO_ARGS2((self, other),
2050 trio_string_t *self,
2051 trio_string_t *other)
2056 return trio_equal_case(self->content, other->content);
2062 * trio_xstring_equal_case
2064 #if defined(TRIO_FUNC_XSTRING_EQUAL_CASE)
2066 TRIO_PUBLIC_STRING int
2067 trio_xstring_equal_case
2068 TRIO_ARGS2((self, other),
2069 trio_string_t *self,
2070 TRIO_CONST char *other)
2075 return trio_equal_case(self->content, other);
2081 * trio_string_equal_case_max
2083 #if defined(TRIO_FUNC_STRING_EQUAL_CASE_MAX)
2085 TRIO_PUBLIC_STRING int
2086 trio_string_equal_case_max
2087 TRIO_ARGS3((self, max, other),
2088 trio_string_t *self,
2090 trio_string_t *other)
2095 return trio_equal_case_max(self->content, max, other->content);
2101 * trio_xstring_equal_case_max
2103 #if defined(TRIO_FUNC_XSTRING_EQUAL_CASE_MAX)
2105 TRIO_PUBLIC_STRING int
2106 trio_xstring_equal_case_max
2107 TRIO_ARGS3((self, max, other),
2108 trio_string_t *self,
2110 TRIO_CONST char *other)
2115 return trio_equal_case_max(self->content, max, other);
2121 * trio_string_format_data_max
2123 #if defined(TRIO_FUNC_STRING_FORMAT_DATE_MAX)
2125 TRIO_PUBLIC_STRING size_t
2126 trio_string_format_date_max
2127 TRIO_ARGS4((self, max, format, datetime),
2128 trio_string_t *self,
2130 TRIO_CONST char *format,
2131 TRIO_CONST struct tm *datetime)
2135 return trio_format_date_max(self->content, max, format, datetime);
2143 #if defined(TRIO_FUNC_STRING_INDEX)
2145 TRIO_PUBLIC_STRING char *
2147 TRIO_ARGS2((self, character),
2148 trio_string_t *self,
2153 return trio_index(self->content, character);
2159 * trio_string_index_last
2161 #if defined(TRIO_FUNC_STRING_INDEX_LAST)
2163 TRIO_PUBLIC_STRING char *
2164 trio_string_index_last
2165 TRIO_ARGS2((self, character),
2166 trio_string_t *self,
2171 return trio_index_last(self->content, character);
2177 * trio_string_length
2179 #if defined(TRIO_FUNC_STRING_LENGTH)
2181 TRIO_PUBLIC_STRING int
2184 trio_string_t *self)
2188 if (self->length == 0)
2190 self->length = trio_length(self->content);
2192 return self->length;
2200 #if defined(TRIO_FUNC_STRING_LOWER)
2202 TRIO_PUBLIC_STRING int
2205 trio_string_t *self)
2209 return trio_lower(self->content);
2217 #if defined(TRIO_FUNC_STRING_MATCH)
2219 TRIO_PUBLIC_STRING int
2221 TRIO_ARGS2((self, other),
2222 trio_string_t *self,
2223 trio_string_t *other)
2228 return trio_match(self->content, other->content);
2234 * trio_xstring_match
2236 #if defined(TRIO_FUNC_XSTRING_MATCH)
2238 TRIO_PUBLIC_STRING int
2240 TRIO_ARGS2((self, other),
2241 trio_string_t *self,
2242 TRIO_CONST char *other)
2247 return trio_match(self->content, other);
2253 * trio_string_match_case
2255 #if defined(TRIO_FUNC_STRING_MATCH_CASE)
2257 TRIO_PUBLIC_STRING int
2258 trio_string_match_case
2259 TRIO_ARGS2((self, other),
2260 trio_string_t *self,
2261 trio_string_t *other)
2266 return trio_match_case(self->content, other->content);
2272 * trio_xstring_match_case
2274 #if defined(TRIO_FUNC_XSTRING_MATCH_CASE)
2276 TRIO_PUBLIC_STRING int
2277 trio_xstring_match_case
2278 TRIO_ARGS2((self, other),
2279 trio_string_t *self,
2280 TRIO_CONST char *other)
2285 return trio_match_case(self->content, other);
2291 * trio_string_substring
2293 #if defined(TRIO_FUNC_STRING_SUBSTRING)
2295 TRIO_PUBLIC_STRING char *
2296 trio_string_substring
2297 TRIO_ARGS2((self, other),
2298 trio_string_t *self,
2299 trio_string_t *other)
2304 return trio_substring(self->content, other->content);
2310 * trio_xstring_substring
2312 #if defined(TRIO_FUNC_XSTRING_SUBSTRING)
2314 TRIO_PUBLIC_STRING char *
2315 trio_xstring_substring
2316 TRIO_ARGS2((self, other),
2317 trio_string_t *self,
2318 TRIO_CONST char *other)
2323 return trio_substring(self->content, other);
2331 #if defined(TRIO_FUNC_STRING_UPPER)
2333 TRIO_PUBLIC_STRING int
2336 trio_string_t *self)
2340 return trio_upper(self->content);
2345 /** @} End of DynamicStrings */