triostr.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*************************************************************************
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg.
  6. *
  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.
  10. *
  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.
  15. *
  16. ************************************************************************/
  17. /*************************************************************************
  18. * Include files
  19. */
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <math.h>
  25. #include "triodef.h"
  26. #include "triostr.h"
  27. /*************************************************************************
  28. * Definitions
  29. */
  30. #if !defined(TRIO_STRING_PUBLIC)
  31. # define TRIO_STRING_PUBLIC TRIO_PUBLIC
  32. #endif
  33. #if !defined(TRIO_STRING_PRIVATE)
  34. # define TRIO_STRING_PRIVATE TRIO_PRIVATE
  35. #endif
  36. #if !defined(NULL)
  37. # define NULL 0
  38. #endif
  39. #if !defined(NIL)
  40. # define NIL ((char)0)
  41. #endif
  42. #if !defined(FALSE)
  43. # define FALSE (1 == 0)
  44. # define TRUE (! FALSE)
  45. #endif
  46. #if !defined(BOOLEAN_T)
  47. # define BOOLEAN_T int
  48. #endif
  49. #if defined(TRIO_COMPILER_SUPPORTS_C99)
  50. # define USE_STRTOD
  51. # define USE_STRTOF
  52. #elif defined(TRIO_COMPILER_MSVC)
  53. # define USE_STRTOD
  54. #endif
  55. #if defined(TRIO_PLATFORM_UNIX)
  56. # define USE_STRCASECMP
  57. # define USE_STRNCASECMP
  58. # if defined(TRIO_PLATFORM_SUNOS)
  59. # define USE_SYS_ERRLIST
  60. # else
  61. # define USE_STRERROR
  62. # endif
  63. # if defined(TRIO_PLATFORM_QNX)
  64. # define strcasecmp(x,y) stricmp(x,y)
  65. # define strncasecmp(x,y,n) strnicmp(x,y,n)
  66. # endif
  67. #elif defined(TRIO_PLATFORM_WIN32)
  68. # define USE_STRCASECMP
  69. # if defined(_WIN32_WCE)
  70. # define strcasecmp(x,y) _stricmp(x,y)
  71. # else
  72. # define strcasecmp(x,y) strcmpi(x,y)
  73. # endif
  74. #endif
  75. #if !(defined(TRIO_PLATFORM_SUNOS))
  76. # define USE_TOLOWER
  77. # define USE_TOUPPER
  78. #endif
  79. /*************************************************************************
  80. * Structures
  81. */
  82. struct _trio_string_t
  83. {
  84. char *content;
  85. size_t length;
  86. size_t allocated;
  87. };
  88. /*************************************************************************
  89. * Constants
  90. */
  91. #if !defined(TRIO_MINIMAL)
  92. static TRIO_CONST char rcsid[] = "@(#)$Id$";
  93. #endif
  94. /*************************************************************************
  95. * Static String Functions
  96. */
  97. #if defined(TRIO_DOCUMENTATION)
  98. # include "doc/doc_static.h"
  99. #endif
  100. /** @addtogroup StaticStrings
  101. @{
  102. */
  103. /**
  104. Create new string.
  105. @param size Size of new string.
  106. @return Pointer to string, or NULL if allocation failed.
  107. */
  108. TRIO_STRING_PUBLIC char *
  109. trio_create
  110. TRIO_ARGS1((size),
  111. size_t size)
  112. {
  113. return (char *)TRIO_MALLOC(size);
  114. }
  115. /**
  116. Destroy string.
  117. @param string String to be freed.
  118. */
  119. TRIO_STRING_PUBLIC void
  120. trio_destroy
  121. TRIO_ARGS1((string),
  122. char *string)
  123. {
  124. if (string)
  125. {
  126. TRIO_FREE(string);
  127. }
  128. }
  129. /**
  130. Count the number of characters in a string.
  131. @param string String to measure.
  132. @return Number of characters in @string.
  133. */
  134. TRIO_STRING_PUBLIC size_t
  135. trio_length
  136. TRIO_ARGS1((string),
  137. TRIO_CONST char *string)
  138. {
  139. return strlen(string);
  140. }
  141. #if !defined(TRIO_MINIMAL)
  142. /**
  143. Append @p source at the end of @p target.
  144. @param target Target string.
  145. @param source Source string.
  146. @return Boolean value indicating success or failure.
  147. @pre @p target must point to a memory chunk with sufficient room to
  148. contain the @p target string and @p source string.
  149. @pre No boundary checking is performed, so insufficient memory will
  150. result in a buffer overrun.
  151. @post @p target will be zero terminated.
  152. */
  153. TRIO_STRING_PUBLIC int
  154. trio_append
  155. TRIO_ARGS2((target, source),
  156. char *target,
  157. TRIO_CONST char *source)
  158. {
  159. assert(target);
  160. assert(source);
  161. return (strcat(target, source) != NULL);
  162. }
  163. #endif /* !defined(TRIO_MINIMAL) */
  164. #if !defined(TRIO_MINIMAL)
  165. /**
  166. Append at most @p max characters from @p source to @p target.
  167. @param target Target string.
  168. @param max Maximum number of characters to append.
  169. @param source Source string.
  170. @return Boolean value indicating success or failure.
  171. @pre @p target must point to a memory chuck with sufficient room to
  172. contain the @p target string and the @p source string (at most @p max
  173. characters).
  174. @pre No boundary checking is performed, so insufficient memory will
  175. result in a buffer overrun.
  176. @post @p target will be zero terminated.
  177. */
  178. TRIO_STRING_PUBLIC int
  179. trio_append_max
  180. TRIO_ARGS3((target, max, source),
  181. char *target,
  182. size_t max,
  183. TRIO_CONST char *source)
  184. {
  185. size_t length;
  186. assert(target);
  187. assert(source);
  188. length = trio_length(target);
  189. if (max > length)
  190. {
  191. strncat(target, source, max - length - 1);
  192. }
  193. return TRUE;
  194. }
  195. #endif /* !defined(TRIO_MINIMAL) */
  196. #if !defined(TRIO_MINIMAL)
  197. /**
  198. Determine if a string contains a substring.
  199. @param string String to be searched.
  200. @param substring String to be found.
  201. @return Boolean value indicating success or failure.
  202. */
  203. TRIO_STRING_PUBLIC int
  204. trio_contains
  205. TRIO_ARGS2((string, substring),
  206. TRIO_CONST char *string,
  207. TRIO_CONST char *substring)
  208. {
  209. assert(string);
  210. assert(substring);
  211. return (0 != strstr(string, substring));
  212. }
  213. #endif /* !defined(TRIO_MINIMAL) */
  214. #if !defined(TRIO_MINIMAL)
  215. /**
  216. Copy @p source to @p target.
  217. @param target Target string.
  218. @param source Source string.
  219. @return Boolean value indicating success or failure.
  220. @pre @p target must point to a memory chunk with sufficient room to
  221. contain the @p source string.
  222. @pre No boundary checking is performed, so insufficient memory will
  223. result in a buffer overrun.
  224. @post @p target will be zero terminated.
  225. */
  226. TRIO_STRING_PUBLIC int
  227. trio_copy
  228. TRIO_ARGS2((target, source),
  229. char *target,
  230. TRIO_CONST char *source)
  231. {
  232. assert(target);
  233. assert(source);
  234. (void)strcpy(target, source);
  235. return TRUE;
  236. }
  237. #endif /* !defined(TRIO_MINIMAL) */
  238. /**
  239. Copy at most @p max characters from @p source to @p target.
  240. @param target Target string.
  241. @param max Maximum number of characters to append.
  242. @param source Source string.
  243. @return Boolean value indicating success or failure.
  244. @pre @p target must point to a memory chunk with sufficient room to
  245. contain the @p source string (at most @p max characters).
  246. @pre No boundary checking is performed, so insufficient memory will
  247. result in a buffer overrun.
  248. @post @p target will be zero terminated.
  249. */
  250. TRIO_STRING_PUBLIC int
  251. trio_copy_max
  252. TRIO_ARGS3((target, max, source),
  253. char *target,
  254. size_t max,
  255. TRIO_CONST char *source)
  256. {
  257. assert(target);
  258. assert(source);
  259. assert(max > 0); /* Includes != 0 */
  260. (void)strncpy(target, source, max - 1);
  261. target[max - 1] = (char)0;
  262. return TRUE;
  263. }
  264. /*
  265. * TrioDuplicateMax
  266. */
  267. TRIO_STRING_PRIVATE char *
  268. TrioDuplicateMax
  269. TRIO_ARGS2((source, size),
  270. TRIO_CONST char *source,
  271. size_t size)
  272. {
  273. char *target;
  274. assert(source);
  275. /* Make room for string plus a terminating zero */
  276. size++;
  277. target = trio_create(size);
  278. if (target)
  279. {
  280. trio_copy_max(target, size, source);
  281. }
  282. return target;
  283. }
  284. /**
  285. Duplicate @p source.
  286. @param source Source string.
  287. @return A copy of the @p source string.
  288. @post @p target will be zero terminated.
  289. */
  290. TRIO_STRING_PUBLIC char *
  291. trio_duplicate
  292. TRIO_ARGS1((source),
  293. TRIO_CONST char *source)
  294. {
  295. return TrioDuplicateMax(source, trio_length(source));
  296. }
  297. #if !defined(TRIO_MINIMAL)
  298. /**
  299. Duplicate at most @p max characters of @p source.
  300. @param source Source string.
  301. @param max Maximum number of characters to duplicate.
  302. @return A copy of the @p source string.
  303. @post @p target will be zero terminated.
  304. */
  305. TRIO_STRING_PUBLIC char *
  306. trio_duplicate_max TRIO_ARGS2((source, max),
  307. TRIO_CONST char *source,
  308. size_t max)
  309. {
  310. size_t length;
  311. assert(source);
  312. assert(max > 0);
  313. length = trio_length(source);
  314. if (length > max)
  315. {
  316. length = max;
  317. }
  318. return TrioDuplicateMax(source, length);
  319. }
  320. #endif /* !defined(TRIO_MINIMAL) */
  321. /**
  322. Compare if two strings are equal.
  323. @param first First string.
  324. @param second Second string.
  325. @return Boolean indicating whether the two strings are equal or not.
  326. Case-insensitive comparison.
  327. */
  328. TRIO_STRING_PUBLIC int
  329. trio_equal
  330. TRIO_ARGS2((first, second),
  331. TRIO_CONST char *first,
  332. TRIO_CONST char *second)
  333. {
  334. assert(first);
  335. assert(second);
  336. if ((first != NULL) && (second != NULL))
  337. {
  338. #if defined(USE_STRCASECMP)
  339. return (0 == strcasecmp(first, second));
  340. #else
  341. while ((*first != NIL) && (*second != NIL))
  342. {
  343. if (trio_to_upper(*first) != trio_to_upper(*second))
  344. {
  345. break;
  346. }
  347. first++;
  348. second++;
  349. }
  350. return ((*first == NIL) && (*second == NIL));
  351. #endif
  352. }
  353. return FALSE;
  354. }
  355. /**
  356. Compare if two strings are equal.
  357. @param first First string.
  358. @param second Second string.
  359. @return Boolean indicating whether the two strings are equal or not.
  360. Case-sensitive comparison.
  361. */
  362. TRIO_STRING_PUBLIC int
  363. trio_equal_case
  364. TRIO_ARGS2((first, second),
  365. TRIO_CONST char *first,
  366. TRIO_CONST char *second)
  367. {
  368. assert(first);
  369. assert(second);
  370. if ((first != NULL) && (second != NULL))
  371. {
  372. return (0 == strcmp(first, second));
  373. }
  374. return FALSE;
  375. }
  376. #if !defined(TRIO_MINIMAL)
  377. /**
  378. Compare if two strings up until the first @p max characters are equal.
  379. @param first First string.
  380. @param max Maximum number of characters to compare.
  381. @param second Second string.
  382. @return Boolean indicating whether the two strings are equal or not.
  383. Case-sensitive comparison.
  384. */
  385. TRIO_STRING_PUBLIC int
  386. trio_equal_case_max
  387. TRIO_ARGS3((first, max, second),
  388. TRIO_CONST char *first,
  389. size_t max,
  390. TRIO_CONST char *second)
  391. {
  392. assert(first);
  393. assert(second);
  394. if ((first != NULL) && (second != NULL))
  395. {
  396. return (0 == strncmp(first, second, max));
  397. }
  398. return FALSE;
  399. }
  400. #endif /* !defined(TRIO_MINIMAL) */
  401. /**
  402. Compare if two strings are equal.
  403. @param first First string.
  404. @param second Second string.
  405. @return Boolean indicating whether the two strings are equal or not.
  406. Collating characters are considered equal.
  407. */
  408. TRIO_STRING_PUBLIC int
  409. trio_equal_locale
  410. TRIO_ARGS2((first, second),
  411. TRIO_CONST char *first,
  412. TRIO_CONST char *second)
  413. {
  414. assert(first);
  415. assert(second);
  416. #if defined(LC_COLLATE)
  417. return (strcoll(first, second) == 0);
  418. #else
  419. return trio_equal(first, second);
  420. #endif
  421. }
  422. /**
  423. Compare if two strings up until the first @p max characters are equal.
  424. @param first First string.
  425. @param max Maximum number of characters to compare.
  426. @param second Second string.
  427. @return Boolean indicating whether the two strings are equal or not.
  428. Case-insensitive comparison.
  429. */
  430. TRIO_STRING_PUBLIC int
  431. trio_equal_max
  432. TRIO_ARGS3((first, max, second),
  433. TRIO_CONST char *first,
  434. size_t max,
  435. TRIO_CONST char *second)
  436. {
  437. assert(first);
  438. assert(second);
  439. if ((first != NULL) && (second != NULL))
  440. {
  441. #if defined(USE_STRNCASECMP)
  442. return (0 == strncasecmp(first, second, max));
  443. #else
  444. /* Not adequately tested yet */
  445. size_t cnt = 0;
  446. while ((*first != NIL) && (*second != NIL) && (cnt <= max))
  447. {
  448. if (trio_to_upper(*first) != trio_to_upper(*second))
  449. {
  450. break;
  451. }
  452. first++;
  453. second++;
  454. cnt++;
  455. }
  456. return ((cnt == max) || ((*first == NIL) && (*second == NIL)));
  457. #endif
  458. }
  459. return FALSE;
  460. }
  461. /**
  462. Provide a textual description of an error code (errno).
  463. @param error_number Error number.
  464. @return Textual description of @p error_number.
  465. */
  466. TRIO_STRING_PUBLIC TRIO_CONST char *
  467. trio_error
  468. TRIO_ARGS1((error_number),
  469. int error_number)
  470. {
  471. #if defined(USE_STRERROR)
  472. return strerror(error_number);
  473. #elif defined(USE_SYS_ERRLIST)
  474. extern char *sys_errlist[];
  475. extern int sys_nerr;
  476. return ((error_number < 0) || (error_number >= sys_nerr))
  477. ? "unknown"
  478. : sys_errlist[error_number];
  479. #else
  480. return "unknown";
  481. #endif
  482. }
  483. #if !defined(TRIO_MINIMAL) && !defined(_WIN32_WCE)
  484. /**
  485. Format the date/time according to @p format.
  486. @param target Target string.
  487. @param max Maximum number of characters to format.
  488. @param format Formatting string.
  489. @param datetime Date/time structure.
  490. @return Number of formatted characters.
  491. The formatting string accepts the same specifiers as the standard C
  492. function strftime.
  493. */
  494. TRIO_STRING_PUBLIC size_t
  495. trio_format_date_max
  496. TRIO_ARGS4((target, max, format, datetime),
  497. char *target,
  498. size_t max,
  499. TRIO_CONST char *format,
  500. TRIO_CONST struct tm *datetime)
  501. {
  502. assert(target);
  503. assert(format);
  504. assert(datetime);
  505. assert(max > 0);
  506. return strftime(target, max, format, datetime);
  507. }
  508. #endif /* !defined(TRIO_MINIMAL) */
  509. #if !defined(TRIO_MINIMAL)
  510. /**
  511. Calculate a hash value for a string.
  512. @param string String to be calculated on.
  513. @param type Hash function.
  514. @return Calculated hash value.
  515. @p type can be one of the following
  516. @li @c TRIO_HASH_PLAIN Plain hash function.
  517. */
  518. TRIO_STRING_PUBLIC unsigned long
  519. trio_hash
  520. TRIO_ARGS2((string, type),
  521. TRIO_CONST char *string,
  522. int type)
  523. {
  524. unsigned long value = 0L;
  525. char ch;
  526. assert(string);
  527. switch (type)
  528. {
  529. case TRIO_HASH_PLAIN:
  530. while ( (ch = *string++) != NIL )
  531. {
  532. value *= 31;
  533. value += (unsigned long)ch;
  534. }
  535. break;
  536. default:
  537. assert(FALSE);
  538. break;
  539. }
  540. return value;
  541. }
  542. #endif /* !defined(TRIO_MINIMAL) */
  543. #if !defined(TRIO_MINIMAL)
  544. /**
  545. Find first occurrence of a character in a string.
  546. @param string String to be searched.
  547. @param character Character to be found.
  548. @param A pointer to the found character, or NULL if character was not found.
  549. */
  550. TRIO_STRING_PUBLIC char *
  551. trio_index
  552. TRIO_ARGS2((string, character),
  553. TRIO_CONST char *string,
  554. int character)
  555. {
  556. assert(string);
  557. return strchr(string, character);
  558. }
  559. #endif /* !defined(TRIO_MINIMAL) */
  560. #if !defined(TRIO_MINIMAL)
  561. /**
  562. Find last occurrence of a character in a string.
  563. @param string String to be searched.
  564. @param character Character to be found.
  565. @param A pointer to the found character, or NULL if character was not found.
  566. */
  567. TRIO_STRING_PUBLIC char *
  568. trio_index_last
  569. TRIO_ARGS2((string, character),
  570. TRIO_CONST char *string,
  571. int character)
  572. {
  573. assert(string);
  574. return strchr(string, character);
  575. }
  576. #endif /* !defined(TRIO_MINIMAL) */
  577. #if !defined(TRIO_MINIMAL)
  578. /**
  579. Convert the alphabetic letters in the string to lower-case.
  580. @param target String to be converted.
  581. @return Number of processed characters (converted or not).
  582. */
  583. TRIO_STRING_PUBLIC int
  584. trio_lower
  585. TRIO_ARGS1((target),
  586. char *target)
  587. {
  588. assert(target);
  589. return trio_span_function(target, target, trio_to_lower);
  590. }
  591. #endif /* !defined(TRIO_MINIMAL) */
  592. #if !defined(TRIO_MINIMAL)
  593. /**
  594. Compare two strings using wildcards.
  595. @param string String to be searched.
  596. @param pattern Pattern, including wildcards, to search for.
  597. @return Boolean value indicating success or failure.
  598. Case-insensitive comparison.
  599. The following wildcards can be used
  600. @li @c * Match any number of characters.
  601. @li @c ? Match a single character.
  602. */
  603. TRIO_STRING_PUBLIC int
  604. trio_match
  605. TRIO_ARGS2((string, pattern),
  606. TRIO_CONST char *string,
  607. TRIO_CONST char *pattern)
  608. {
  609. assert(string);
  610. assert(pattern);
  611. for (; ('*' != *pattern); ++pattern, ++string)
  612. {
  613. if (NIL == *string)
  614. {
  615. return (NIL == *pattern);
  616. }
  617. if ((trio_to_upper((int)*string) != trio_to_upper((int)*pattern))
  618. && ('?' != *pattern))
  619. {
  620. return FALSE;
  621. }
  622. }
  623. /* two-line patch to prevent *too* much recursiveness: */
  624. while ('*' == pattern[1])
  625. pattern++;
  626. do
  627. {
  628. if ( trio_match(string, &pattern[1]) )
  629. {
  630. return TRUE;
  631. }
  632. }
  633. while (*string++);
  634. return FALSE;
  635. }
  636. #endif /* !defined(TRIO_MINIMAL) */
  637. #if !defined(TRIO_MINIMAL)
  638. /**
  639. Compare two strings using wildcards.
  640. @param string String to be searched.
  641. @param pattern Pattern, including wildcards, to search for.
  642. @return Boolean value indicating success or failure.
  643. Case-sensitive comparison.
  644. The following wildcards can be used
  645. @li @c * Match any number of characters.
  646. @li @c ? Match a single character.
  647. */
  648. TRIO_STRING_PUBLIC int
  649. trio_match_case
  650. TRIO_ARGS2((string, pattern),
  651. TRIO_CONST char *string,
  652. TRIO_CONST char *pattern)
  653. {
  654. assert(string);
  655. assert(pattern);
  656. for (; ('*' != *pattern); ++pattern, ++string)
  657. {
  658. if (NIL == *string)
  659. {
  660. return (NIL == *pattern);
  661. }
  662. if ((*string != *pattern)
  663. && ('?' != *pattern))
  664. {
  665. return FALSE;
  666. }
  667. }
  668. /* two-line patch to prevent *too* much recursiveness: */
  669. while ('*' == pattern[1])
  670. pattern++;
  671. do
  672. {
  673. if ( trio_match_case(string, &pattern[1]) )
  674. {
  675. return TRUE;
  676. }
  677. }
  678. while (*string++);
  679. return FALSE;
  680. }
  681. #endif /* !defined(TRIO_MINIMAL) */
  682. #if !defined(TRIO_MINIMAL)
  683. /**
  684. Execute a function on each character in string.
  685. @param target Target string.
  686. @param source Source string.
  687. @param Function Function to be executed.
  688. @return Number of processed characters.
  689. */
  690. TRIO_STRING_PUBLIC size_t
  691. trio_span_function
  692. TRIO_ARGS3((target, source, Function),
  693. char *target,
  694. TRIO_CONST char *source,
  695. int (*Function) TRIO_PROTO((int)))
  696. {
  697. size_t count = 0;
  698. assert(target);
  699. assert(source);
  700. assert(Function);
  701. while (*source != NIL)
  702. {
  703. *target++ = Function(*source++);
  704. count++;
  705. }
  706. return count;
  707. }
  708. #endif /* !defined(TRIO_MINIMAL) */
  709. #if !defined(TRIO_MINIMAL)
  710. /**
  711. Search for a substring in a string.
  712. @param string String to be searched.
  713. @param substring String to be found.
  714. @return Pointer to first occurrence of @p substring in @p string, or NULL
  715. if no match was found.
  716. */
  717. TRIO_STRING_PUBLIC char *
  718. trio_substring
  719. TRIO_ARGS2((string, substring),
  720. TRIO_CONST char *string,
  721. TRIO_CONST char *substring)
  722. {
  723. assert(string);
  724. assert(substring);
  725. return strstr(string, substring);
  726. }
  727. #endif /* !defined(TRIO_MINIMAL) */
  728. #if !defined(TRIO_MINIMAL)
  729. /**
  730. Search for a substring in the first @p max characters of a string.
  731. @param string String to be searched.
  732. @param max Maximum characters to be searched.
  733. @param substring String to be found.
  734. @return Pointer to first occurrence of @p substring in @p string, or NULL
  735. if no match was found.
  736. */
  737. TRIO_STRING_PUBLIC char *
  738. trio_substring_max
  739. TRIO_ARGS3((string, max, substring),
  740. TRIO_CONST char *string,
  741. size_t max,
  742. TRIO_CONST char *substring)
  743. {
  744. size_t count;
  745. size_t size;
  746. char *result = NULL;
  747. assert(string);
  748. assert(substring);
  749. size = trio_length(substring);
  750. if (size <= max)
  751. {
  752. for (count = 0; count <= max - size; count++)
  753. {
  754. if (trio_equal_max(substring, size, &string[count]))
  755. {
  756. result = (char *)&string[count];
  757. break;
  758. }
  759. }
  760. }
  761. return result;
  762. }
  763. #endif /* !defined(TRIO_MINIMAL) */
  764. #if !defined(TRIO_MINIMAL)
  765. /**
  766. Tokenize string.
  767. @param string String to be tokenized.
  768. @param tokens String containing list of delimiting characters.
  769. @return Start of new token.
  770. @warning @p string will be destroyed.
  771. */
  772. TRIO_STRING_PUBLIC char *
  773. trio_tokenize
  774. TRIO_ARGS2((string, delimiters),
  775. char *string,
  776. TRIO_CONST char *delimiters)
  777. {
  778. assert(delimiters);
  779. return strtok(string, delimiters);
  780. }
  781. #endif /* !defined(TRIO_MINIMAL) */
  782. /**
  783. Convert string to floating-point number.
  784. @param source String to be converted.
  785. @param endp Pointer to end of the converted string.
  786. @return A floating-point number.
  787. The following Extended Backus-Naur form is used
  788. @verbatim
  789. double ::= [ <sign> ]
  790. ( <number> |
  791. <number> <decimal_point> <number> |
  792. <decimal_point> <number> )
  793. [ <exponential> [ <sign> ] <number> ]
  794. number ::= 1*( <digit> )
  795. digit ::= ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )
  796. exponential ::= ( 'e' | 'E' )
  797. sign ::= ( '-' | '+' )
  798. decimal_point ::= '.'
  799. @endverbatim
  800. */
  801. /* FIXME: Add EBNF for hex-floats */
  802. TRIO_STRING_PUBLIC trio_long_double_t
  803. trio_to_long_double
  804. TRIO_ARGS2((source, endp),
  805. TRIO_CONST char *source,
  806. char **endp)
  807. {
  808. #if defined(USE_STRTOLD)
  809. return strtold(source, endp);
  810. #else
  811. int isNegative = FALSE;
  812. int isExponentNegative = FALSE;
  813. trio_long_double_t integer = 0.0;
  814. trio_long_double_t fraction = 0.0;
  815. unsigned long exponent = 0;
  816. trio_long_double_t base;
  817. trio_long_double_t fracdiv = 1.0;
  818. trio_long_double_t value = 0.0;
  819. /* First try hex-floats */
  820. if ((source[0] == '0') && ((source[1] == 'x') || (source[1] == 'X')))
  821. {
  822. base = 16.0;
  823. source += 2;
  824. while (isxdigit((int)*source))
  825. {
  826. integer *= base;
  827. integer += (isdigit((int)*source)
  828. ? (*source - '0')
  829. : 10 + (trio_to_upper((int)*source) - 'A'));
  830. source++;
  831. }
  832. if (*source == '.')
  833. {
  834. source++;
  835. while (isxdigit((int)*source))
  836. {
  837. fracdiv /= base;
  838. fraction += fracdiv * (isdigit((int)*source)
  839. ? (*source - '0')
  840. : 10 + (trio_to_upper((int)*source) - 'A'));
  841. source++;
  842. }
  843. if ((*source == 'p') || (*source == 'P'))
  844. {
  845. source++;
  846. if ((*source == '+') || (*source == '-'))
  847. {
  848. isExponentNegative = (*source == '-');
  849. source++;
  850. }
  851. while (isdigit((int)*source))
  852. {
  853. exponent *= 10;
  854. exponent += (*source - '0');
  855. source++;
  856. }
  857. }
  858. }
  859. /* For later use with exponent */
  860. base = 2.0;
  861. }
  862. else /* Then try normal decimal floats */
  863. {
  864. base = 10.0;
  865. isNegative = (*source == '-');
  866. /* Skip sign */
  867. if ((*source == '+') || (*source == '-'))
  868. source++;
  869. /* Integer part */
  870. while (isdigit((int)*source))
  871. {
  872. integer *= base;
  873. integer += (*source - '0');
  874. source++;
  875. }
  876. if (*source == '.')
  877. {
  878. source++; /* skip decimal point */
  879. while (isdigit((int)*source))
  880. {
  881. fracdiv /= base;
  882. fraction += (*source - '0') * fracdiv;
  883. source++;
  884. }
  885. }
  886. if ((*source == 'e')
  887. || (*source == 'E')
  888. #if TRIO_MICROSOFT
  889. || (*source == 'd')
  890. || (*source == 'D')
  891. #endif
  892. )
  893. {
  894. source++; /* Skip exponential indicator */
  895. isExponentNegative = (*source == '-');
  896. if ((*source == '+') || (*source == '-'))
  897. source++;
  898. while (isdigit((int)*source))
  899. {
  900. exponent *= (int)base;
  901. exponent += (*source - '0');
  902. source++;
  903. }
  904. }
  905. }
  906. value = integer + fraction;
  907. if (exponent != 0)
  908. {
  909. if (isExponentNegative)
  910. value /= pow(base, (double)exponent);
  911. else
  912. value *= pow(base, (double)exponent);
  913. }
  914. if (isNegative)
  915. value = -value;
  916. if (endp)
  917. *endp = (char *)source;
  918. return value;
  919. #endif
  920. }
  921. /**
  922. Convert string to floating-point number.
  923. @param source String to be converted.
  924. @param endp Pointer to end of the converted string.
  925. @return A floating-point number.
  926. See @ref trio_to_long_double.
  927. */
  928. TRIO_STRING_PUBLIC double
  929. trio_to_double
  930. TRIO_ARGS2((source, endp),
  931. TRIO_CONST char *source,
  932. char **endp)
  933. {
  934. #if defined(USE_STRTOD)
  935. return strtod(source, endp);
  936. #else
  937. return (double)trio_to_long_double(source, endp);
  938. #endif
  939. }
  940. #if !defined(TRIO_MINIMAL)
  941. /**
  942. Convert string to floating-point number.
  943. @param source String to be converted.
  944. @param endp Pointer to end of the converted string.
  945. @return A floating-point number.
  946. See @ref trio_to_long_double.
  947. */
  948. TRIO_STRING_PUBLIC float
  949. trio_to_float
  950. TRIO_ARGS2((source, endp),
  951. TRIO_CONST char *source,
  952. char **endp)
  953. {
  954. #if defined(USE_STRTOF)
  955. return strtof(source, endp);
  956. #else
  957. return (float)trio_to_long_double(source, endp);
  958. #endif
  959. }
  960. #endif /* !defined(TRIO_MINIMAL) */
  961. /**
  962. Convert string to signed integer.
  963. @param string String to be converted.
  964. @param endp Pointer to end of converted string.
  965. @param base Radix number of number.
  966. */
  967. TRIO_STRING_PUBLIC long
  968. trio_to_long
  969. TRIO_ARGS3((string, endp, base),
  970. TRIO_CONST char *string,
  971. char **endp,
  972. int base)
  973. {
  974. assert(string);
  975. assert((base >= 2) && (base <= 36));
  976. return strtol(string, endp, base);
  977. }
  978. #if !defined(TRIO_MINIMAL)
  979. /**
  980. Convert one alphabetic letter to lower-case.
  981. @param source The letter to be converted.
  982. @return The converted letter.
  983. */
  984. TRIO_STRING_PUBLIC int
  985. trio_to_lower
  986. TRIO_ARGS1((source),
  987. int source)
  988. {
  989. #if defined(USE_TOLOWER)
  990. return tolower(source);
  991. #else
  992. /* Does not handle locales or non-contiguous alphabetic characters */
  993. return ((source >= (int)'A') && (source <= (int)'Z'))
  994. ? source - 'A' + 'a'
  995. : source;
  996. #endif
  997. }
  998. #endif /* !defined(TRIO_MINIMAL) */
  999. #if !defined(TRIO_MINIMAL)
  1000. /**
  1001. Convert string to unsigned integer.
  1002. @param string String to be converted.
  1003. @param endp Pointer to end of converted string.
  1004. @param base Radix number of number.
  1005. */
  1006. TRIO_STRING_PUBLIC unsigned long
  1007. trio_to_unsigned_long
  1008. TRIO_ARGS3((string, endp, base),
  1009. TRIO_CONST char *string,
  1010. char **endp,
  1011. int base)
  1012. {
  1013. assert(string);
  1014. assert((base >= 2) && (base <= 36));
  1015. return strtoul(string, endp, base);
  1016. }
  1017. #endif /* !defined(TRIO_MINIMAL) */
  1018. /**
  1019. Convert one alphabetic letter to upper-case.
  1020. @param source The letter to be converted.
  1021. @return The converted letter.
  1022. */
  1023. TRIO_STRING_PUBLIC int
  1024. trio_to_upper
  1025. TRIO_ARGS1((source),
  1026. int source)
  1027. {
  1028. #if defined(USE_TOUPPER)
  1029. return toupper(source);
  1030. #else
  1031. /* Does not handle locales or non-contiguous alphabetic characters */
  1032. return ((source >= (int)'a') && (source <= (int)'z'))
  1033. ? source - 'a' + 'A'
  1034. : source;
  1035. #endif
  1036. }
  1037. #if !defined(TRIO_MINIMAL)
  1038. /**
  1039. Convert the alphabetic letters in the string to upper-case.
  1040. @param target The string to be converted.
  1041. @return The number of processed characters (converted or not).
  1042. */
  1043. TRIO_STRING_PUBLIC int
  1044. trio_upper
  1045. TRIO_ARGS1((target),
  1046. char *target)
  1047. {
  1048. assert(target);
  1049. return trio_span_function(target, target, trio_to_upper);
  1050. }
  1051. #endif /* !defined(TRIO_MINIMAL) */
  1052. /** @} End of StaticStrings */
  1053. /*************************************************************************
  1054. * Dynamic String Functions
  1055. */
  1056. #if defined(TRIO_DOCUMENTATION)
  1057. # include "doc/doc_dynamic.h"
  1058. #endif
  1059. /** @addtogroup DynamicStrings
  1060. @{
  1061. */
  1062. /*
  1063. * TrioStringAlloc
  1064. */
  1065. TRIO_STRING_PRIVATE trio_string_t *
  1066. TrioStringAlloc(TRIO_NOARGS)
  1067. {
  1068. trio_string_t *self;
  1069. self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t));
  1070. if (self)
  1071. {
  1072. self->content = NULL;
  1073. self->length = 0;
  1074. self->allocated = 0;
  1075. }
  1076. return self;
  1077. }
  1078. /*
  1079. * TrioStringGrow
  1080. *
  1081. * The size of the string will be increased by 'delta' characters. If
  1082. * 'delta' is zero, the size will be doubled.
  1083. */
  1084. TRIO_STRING_PRIVATE BOOLEAN_T
  1085. TrioStringGrow
  1086. TRIO_ARGS2((self, delta),
  1087. trio_string_t *self,
  1088. size_t delta)
  1089. {
  1090. BOOLEAN_T status = FALSE;
  1091. char *new_content;
  1092. size_t new_size;
  1093. new_size = (delta == 0)
  1094. ? ( (self->allocated == 0) ? 1 : self->allocated * 2 )
  1095. : self->allocated + delta;
  1096. new_content = (char *)TRIO_REALLOC(self->content, new_size);
  1097. if (new_content)
  1098. {
  1099. self->content = new_content;
  1100. self->allocated = new_size;
  1101. status = TRUE;
  1102. }
  1103. return status;
  1104. }
  1105. #if !defined(TRIO_MINIMAL)
  1106. /*
  1107. * TrioStringGrowTo
  1108. *
  1109. * The size of the string will be increased to 'length' plus one characters.
  1110. * If 'length' is less than the original size, the original size will be
  1111. * used (that is, the size of the string is never decreased).
  1112. */
  1113. TRIO_STRING_PRIVATE BOOLEAN_T
  1114. TrioStringGrowTo
  1115. TRIO_ARGS2((self, length),
  1116. trio_string_t *self,
  1117. size_t length)
  1118. {
  1119. length++; /* Room for terminating zero */
  1120. return (self->allocated < length)
  1121. ? TrioStringGrow(self, length - self->allocated)
  1122. : TRUE;
  1123. }
  1124. #endif /* !defined(TRIO_MINIMAL) */
  1125. #if !defined(TRIO_MINIMAL)
  1126. /**
  1127. Create a new dynamic string.
  1128. @param initial_size Initial size of the buffer.
  1129. @return Newly allocated dynamic string, or NULL if memory allocation failed.
  1130. */
  1131. TRIO_STRING_PUBLIC trio_string_t *
  1132. trio_string_create
  1133. TRIO_ARGS1((initial_size),
  1134. int initial_size)
  1135. {
  1136. trio_string_t *self;
  1137. self = TrioStringAlloc();
  1138. if (self)
  1139. {
  1140. if (TrioStringGrow(self,
  1141. (size_t)((initial_size > 0) ? initial_size : 1)))
  1142. {
  1143. self->content[0] = (char)0;
  1144. self->allocated = initial_size;
  1145. }
  1146. else
  1147. {
  1148. trio_string_destroy(self);
  1149. self = NULL;
  1150. }
  1151. }
  1152. return self;
  1153. }
  1154. #endif /* !defined(TRIO_MINIMAL) */
  1155. /**
  1156. Deallocate the dynamic string and its contents.
  1157. @param self Dynamic string
  1158. */
  1159. TRIO_STRING_PUBLIC void
  1160. trio_string_destroy
  1161. TRIO_ARGS1((self),
  1162. trio_string_t *self)
  1163. {
  1164. assert(self);
  1165. if (self)
  1166. {
  1167. trio_destroy(self->content);
  1168. TRIO_FREE(self);
  1169. }
  1170. }
  1171. #if !defined(TRIO_MINIMAL)
  1172. /**
  1173. Get a pointer to the content.
  1174. @param self Dynamic string.
  1175. @param offset Offset into content.
  1176. @return Pointer to the content.
  1177. @p Offset can be zero, positive, or negative. If @p offset is zero,
  1178. then the start of the content will be returned. If @p offset is positive,
  1179. then a pointer to @p offset number of characters from the beginning of the
  1180. content is returned. If @p offset is negative, then a pointer to @p offset
  1181. number of characters from the ending of the string, starting at the
  1182. terminating zero, is returned.
  1183. */
  1184. TRIO_STRING_PUBLIC char *
  1185. trio_string_get
  1186. TRIO_ARGS2((self, offset),
  1187. trio_string_t *self,
  1188. int offset)
  1189. {
  1190. char *result = NULL;
  1191. assert(self);
  1192. if (self->content != NULL)
  1193. {
  1194. if (self->length == 0)
  1195. {
  1196. (void)trio_string_length(self);
  1197. }
  1198. if (offset >= 0)
  1199. {
  1200. if (offset > (int)self->length)
  1201. {
  1202. offset = self->length;
  1203. }
  1204. }
  1205. else
  1206. {
  1207. offset += self->length + 1;
  1208. if (offset < 0)
  1209. {
  1210. offset = 0;
  1211. }
  1212. }
  1213. result = &(self->content[offset]);
  1214. }
  1215. return result;
  1216. }
  1217. #endif /* !defined(TRIO_MINIMAL) */
  1218. /**
  1219. Extract the content.
  1220. @param self Dynamic String
  1221. @return Content of dynamic string.
  1222. The content is removed from the dynamic string. This enables destruction
  1223. of the dynamic string without deallocation of the content.
  1224. */
  1225. TRIO_STRING_PUBLIC char *
  1226. trio_string_extract
  1227. TRIO_ARGS1((self),
  1228. trio_string_t *self)
  1229. {
  1230. char *result;
  1231. assert(self);
  1232. result = self->content;
  1233. /* FIXME: Allocate new empty buffer? */
  1234. self->content = NULL;
  1235. self->length = self->allocated = 0;
  1236. return result;
  1237. }
  1238. #if !defined(TRIO_MINIMAL)
  1239. /**
  1240. Set the content of the dynamic string.
  1241. @param self Dynamic String
  1242. @param buffer The new content.
  1243. Sets the content of the dynamic string to a copy @p buffer.
  1244. An existing content will be deallocated first, if necessary.
  1245. @remark
  1246. This function will make a copy of @p buffer.
  1247. You are responsible for deallocating @p buffer yourself.
  1248. */
  1249. TRIO_STRING_PUBLIC void
  1250. trio_xstring_set
  1251. TRIO_ARGS2((self, buffer),
  1252. trio_string_t *self,
  1253. char *buffer)
  1254. {
  1255. assert(self);
  1256. trio_destroy(self->content);
  1257. self->content = trio_duplicate(buffer);
  1258. }
  1259. #endif /* !defined(TRIO_MINIMAL) */
  1260. /*
  1261. * trio_string_size
  1262. */
  1263. TRIO_STRING_PUBLIC int
  1264. trio_string_size
  1265. TRIO_ARGS1((self),
  1266. trio_string_t *self)
  1267. {
  1268. assert(self);
  1269. return self->allocated;
  1270. }
  1271. /*
  1272. * trio_string_terminate
  1273. */
  1274. TRIO_STRING_PUBLIC void
  1275. trio_string_terminate
  1276. TRIO_ARGS1((self),
  1277. trio_string_t *self)
  1278. {
  1279. trio_xstring_append_char(self, 0);
  1280. }
  1281. #if !defined(TRIO_MINIMAL)
  1282. /**
  1283. Append the second string to the first.
  1284. @param self Dynamic string to be modified.
  1285. @param other Dynamic string to copy from.
  1286. @return Boolean value indicating success or failure.
  1287. */
  1288. TRIO_STRING_PUBLIC int
  1289. trio_string_append
  1290. TRIO_ARGS2((self, other),
  1291. trio_string_t *self,
  1292. trio_string_t *other)
  1293. {
  1294. size_t length;
  1295. assert(self);
  1296. assert(other);
  1297. length = self->length + other->length;
  1298. if (!TrioStringGrowTo(self, length))
  1299. goto error;
  1300. trio_copy(&self->content[self->length], other->content);
  1301. self->length = length;
  1302. return TRUE;
  1303. error:
  1304. return FALSE;
  1305. }
  1306. #endif /* !defined(TRIO_MINIMAL) */
  1307. #if !defined(TRIO_MINIMAL)
  1308. /*
  1309. * trio_xstring_append
  1310. */
  1311. TRIO_STRING_PUBLIC int
  1312. trio_xstring_append
  1313. TRIO_ARGS2((self, other),
  1314. trio_string_t *self,
  1315. TRIO_CONST char *other)
  1316. {
  1317. size_t length;
  1318. assert(self);
  1319. assert(other);
  1320. length = self->length + trio_length(other);
  1321. if (!TrioStringGrowTo(self, length))
  1322. goto error;
  1323. trio_copy(&self->content[self->length], other);
  1324. self->length = length;
  1325. return TRUE;
  1326. error:
  1327. return FALSE;
  1328. }
  1329. #endif /* !defined(TRIO_MINIMAL) */
  1330. /*
  1331. * trio_xstring_append_char
  1332. */
  1333. TRIO_STRING_PUBLIC int
  1334. trio_xstring_append_char
  1335. TRIO_ARGS2((self, character),
  1336. trio_string_t *self,
  1337. char character)
  1338. {
  1339. assert(self);
  1340. if ((int)self->length >= trio_string_size(self))
  1341. {
  1342. if (!TrioStringGrow(self, 0))
  1343. goto error;
  1344. }
  1345. self->content[self->length] = character;
  1346. self->length++;
  1347. return TRUE;
  1348. error:
  1349. return FALSE;
  1350. }
  1351. #if !defined(TRIO_MINIMAL)
  1352. /**
  1353. Search for the first occurrence of second parameter in the first.
  1354. @param self Dynamic string to be modified.
  1355. @param other Dynamic string to copy from.
  1356. @return Boolean value indicating success or failure.
  1357. */
  1358. TRIO_STRING_PUBLIC int
  1359. trio_string_contains
  1360. TRIO_ARGS2((self, other),
  1361. trio_string_t *self,
  1362. trio_string_t *other)
  1363. {
  1364. assert(self);
  1365. assert(other);
  1366. return trio_contains(self->content, other->content);
  1367. }
  1368. #endif /* !defined(TRIO_MINIMAL) */
  1369. #if !defined(TRIO_MINIMAL)
  1370. /*
  1371. * trio_xstring_contains
  1372. */
  1373. TRIO_STRING_PUBLIC int
  1374. trio_xstring_contains
  1375. TRIO_ARGS2((self, other),
  1376. trio_string_t *self,
  1377. TRIO_CONST char *other)
  1378. {
  1379. assert(self);
  1380. assert(other);
  1381. return trio_contains(self->content, other);
  1382. }
  1383. #endif /* !defined(TRIO_MINIMAL) */
  1384. #if !defined(TRIO_MINIMAL)
  1385. /*
  1386. * trio_string_copy
  1387. */
  1388. TRIO_STRING_PUBLIC int
  1389. trio_string_copy
  1390. TRIO_ARGS2((self, other),
  1391. trio_string_t *self,
  1392. trio_string_t *other)
  1393. {
  1394. assert(self);
  1395. assert(other);
  1396. self->length = 0;
  1397. return trio_string_append(self, other);
  1398. }
  1399. #endif /* !defined(TRIO_MINIMAL) */
  1400. #if !defined(TRIO_MINIMAL)
  1401. /*
  1402. * trio_xstring_copy
  1403. */
  1404. TRIO_STRING_PUBLIC int
  1405. trio_xstring_copy
  1406. TRIO_ARGS2((self, other),
  1407. trio_string_t *self,
  1408. TRIO_CONST char *other)
  1409. {
  1410. assert(self);
  1411. assert(other);
  1412. self->length = 0;
  1413. return trio_xstring_append(self, other);
  1414. }
  1415. #endif /* !defined(TRIO_MINIMAL) */
  1416. #if !defined(TRIO_MINIMAL)
  1417. /*
  1418. * trio_string_duplicate
  1419. */
  1420. TRIO_STRING_PUBLIC trio_string_t *
  1421. trio_string_duplicate
  1422. TRIO_ARGS1((other),
  1423. trio_string_t *other)
  1424. {
  1425. trio_string_t *self;
  1426. assert(other);
  1427. self = TrioStringAlloc();
  1428. if (self)
  1429. {
  1430. self->content = TrioDuplicateMax(other->content, other->length);
  1431. if (self->content)
  1432. {
  1433. self->length = other->length;
  1434. self->allocated = self->length + 1;
  1435. }
  1436. else
  1437. {
  1438. self->length = self->allocated = 0;
  1439. }
  1440. }
  1441. return self;
  1442. }
  1443. #endif /* !defined(TRIO_MINIMAL) */
  1444. /*
  1445. * trio_xstring_duplicate
  1446. */
  1447. TRIO_STRING_PUBLIC trio_string_t *
  1448. trio_xstring_duplicate
  1449. TRIO_ARGS1((other),
  1450. TRIO_CONST char *other)
  1451. {
  1452. trio_string_t *self;
  1453. assert(other);
  1454. self = TrioStringAlloc();
  1455. if (self)
  1456. {
  1457. self->content = TrioDuplicateMax(other, trio_length(other));
  1458. if (self->content)
  1459. {
  1460. self->length = trio_length(self->content);
  1461. self->allocated = self->length + 1;
  1462. }
  1463. else
  1464. {
  1465. self->length = self->allocated = 0;
  1466. }
  1467. }
  1468. return self;
  1469. }
  1470. #if !defined(TRIO_MINIMAL)
  1471. /*
  1472. * trio_string_equal
  1473. */
  1474. TRIO_STRING_PUBLIC int
  1475. trio_string_equal
  1476. TRIO_ARGS2((self, other),
  1477. trio_string_t *self,
  1478. trio_string_t *other)
  1479. {
  1480. assert(self);
  1481. assert(other);
  1482. return trio_equal(self->content, other->content);
  1483. }
  1484. #endif /* !defined(TRIO_MINIMAL) */
  1485. #if !defined(TRIO_MINIMAL)
  1486. /*
  1487. * trio_xstring_equal
  1488. */
  1489. TRIO_STRING_PUBLIC int
  1490. trio_xstring_equal
  1491. TRIO_ARGS2((self, other),
  1492. trio_string_t *self,
  1493. TRIO_CONST char *other)
  1494. {
  1495. assert(self);
  1496. assert(other);
  1497. return trio_equal(self->content, other);
  1498. }
  1499. #endif /* !defined(TRIO_MINIMAL) */
  1500. #if !defined(TRIO_MINIMAL)
  1501. /*
  1502. * trio_string_equal_max
  1503. */
  1504. TRIO_STRING_PUBLIC int
  1505. trio_string_equal_max
  1506. TRIO_ARGS3((self, max, other),
  1507. trio_string_t *self,
  1508. size_t max,
  1509. trio_string_t *other)
  1510. {
  1511. assert(self);
  1512. assert(other);
  1513. return trio_equal_max(self->content, max, other->content);
  1514. }
  1515. #endif /* !defined(TRIO_MINIMAL) */
  1516. #if !defined(TRIO_MINIMAL)
  1517. /*
  1518. * trio_xstring_equal_max
  1519. */
  1520. TRIO_STRING_PUBLIC int
  1521. trio_xstring_equal_max
  1522. TRIO_ARGS3((self, max, other),
  1523. trio_string_t *self,
  1524. size_t max,
  1525. TRIO_CONST char *other)
  1526. {
  1527. assert(self);
  1528. assert(other);
  1529. return trio_equal_max(self->content, max, other);
  1530. }
  1531. #endif /* !defined(TRIO_MINIMAL) */
  1532. #if !defined(TRIO_MINIMAL)
  1533. /*
  1534. * trio_string_equal_case
  1535. */
  1536. TRIO_STRING_PUBLIC int
  1537. trio_string_equal_case
  1538. TRIO_ARGS2((self, other),
  1539. trio_string_t *self,
  1540. trio_string_t *other)
  1541. {
  1542. assert(self);
  1543. assert(other);
  1544. return trio_equal_case(self->content, other->content);
  1545. }
  1546. #endif /* !defined(TRIO_MINIMAL) */
  1547. #if !defined(TRIO_MINIMAL)
  1548. /*
  1549. * trio_xstring_equal_case
  1550. */
  1551. TRIO_STRING_PUBLIC int
  1552. trio_xstring_equal_case
  1553. TRIO_ARGS2((self, other),
  1554. trio_string_t *self,
  1555. TRIO_CONST char *other)
  1556. {
  1557. assert(self);
  1558. assert(other);
  1559. return trio_equal_case(self->content, other);
  1560. }
  1561. #endif /* !defined(TRIO_MINIMAL) */
  1562. #if !defined(TRIO_MINIMAL)
  1563. /*
  1564. * trio_string_equal_case_max
  1565. */
  1566. TRIO_STRING_PUBLIC int
  1567. trio_string_equal_case_max
  1568. TRIO_ARGS3((self, max, other),
  1569. trio_string_t *self,
  1570. size_t max,
  1571. trio_string_t *other)
  1572. {
  1573. assert(self);
  1574. assert(other);
  1575. return trio_equal_case_max(self->content, max, other->content);
  1576. }
  1577. #endif /* !defined(TRIO_MINIMAL) */
  1578. #if !defined(TRIO_MINIMAL)
  1579. /*
  1580. * trio_xstring_equal_case_max
  1581. */
  1582. TRIO_STRING_PUBLIC int
  1583. trio_xstring_equal_case_max
  1584. TRIO_ARGS3((self, max, other),
  1585. trio_string_t *self,
  1586. size_t max,
  1587. TRIO_CONST char *other)
  1588. {
  1589. assert(self);
  1590. assert(other);
  1591. return trio_equal_case_max(self->content, max, other);
  1592. }
  1593. #endif /* !defined(TRIO_MINIMAL) */
  1594. #if !defined(TRIO_MINIMAL) && !defined(_WIN32_WCE)
  1595. /*
  1596. * trio_string_format_data_max
  1597. */
  1598. TRIO_STRING_PUBLIC size_t
  1599. trio_string_format_date_max
  1600. TRIO_ARGS4((self, max, format, datetime),
  1601. trio_string_t *self,
  1602. size_t max,
  1603. TRIO_CONST char *format,
  1604. TRIO_CONST struct tm *datetime)
  1605. {
  1606. assert(self);
  1607. return trio_format_date_max(self->content, max, format, datetime);
  1608. }
  1609. #endif /* !defined(TRIO_MINIMAL) */
  1610. #if !defined(TRIO_MINIMAL)
  1611. /*
  1612. * trio_string_index
  1613. */
  1614. TRIO_STRING_PUBLIC char *
  1615. trio_string_index
  1616. TRIO_ARGS2((self, character),
  1617. trio_string_t *self,
  1618. int character)
  1619. {
  1620. assert(self);
  1621. return trio_index(self->content, character);
  1622. }
  1623. #endif /* !defined(TRIO_MINIMAL) */
  1624. #if !defined(TRIO_MINIMAL)
  1625. /*
  1626. * trio_string_index_last
  1627. */
  1628. TRIO_STRING_PUBLIC char *
  1629. trio_string_index_last
  1630. TRIO_ARGS2((self, character),
  1631. trio_string_t *self,
  1632. int character)
  1633. {
  1634. assert(self);
  1635. return trio_index_last(self->content, character);
  1636. }
  1637. #endif /* !defined(TRIO_MINIMAL) */
  1638. #if !defined(TRIO_MINIMAL)
  1639. /*
  1640. * trio_string_length
  1641. */
  1642. TRIO_STRING_PUBLIC int
  1643. trio_string_length
  1644. TRIO_ARGS1((self),
  1645. trio_string_t *self)
  1646. {
  1647. assert(self);
  1648. if (self->length == 0)
  1649. {
  1650. self->length = trio_length(self->content);
  1651. }
  1652. return self->length;
  1653. }
  1654. #endif /* !defined(TRIO_MINIMAL) */
  1655. #if !defined(TRIO_MINIMAL)
  1656. /*
  1657. * trio_string_lower
  1658. */
  1659. TRIO_STRING_PUBLIC int
  1660. trio_string_lower
  1661. TRIO_ARGS1((self),
  1662. trio_string_t *self)
  1663. {
  1664. assert(self);
  1665. return trio_lower(self->content);
  1666. }
  1667. #endif /* !defined(TRIO_MINIMAL) */
  1668. #if !defined(TRIO_MINIMAL)
  1669. /*
  1670. * trio_string_match
  1671. */
  1672. TRIO_STRING_PUBLIC int
  1673. trio_string_match
  1674. TRIO_ARGS2((self, other),
  1675. trio_string_t *self,
  1676. trio_string_t *other)
  1677. {
  1678. assert(self);
  1679. assert(other);
  1680. return trio_match(self->content, other->content);
  1681. }
  1682. #endif /* !defined(TRIO_MINIMAL) */
  1683. #if !defined(TRIO_MINIMAL)
  1684. /*
  1685. * trio_xstring_match
  1686. */
  1687. TRIO_STRING_PUBLIC int
  1688. trio_xstring_match
  1689. TRIO_ARGS2((self, other),
  1690. trio_string_t *self,
  1691. TRIO_CONST char *other)
  1692. {
  1693. assert(self);
  1694. assert(other);
  1695. return trio_match(self->content, other);
  1696. }
  1697. #endif /* !defined(TRIO_MINIMAL) */
  1698. #if !defined(TRIO_MINIMAL)
  1699. /*
  1700. * trio_string_match_case
  1701. */
  1702. TRIO_STRING_PUBLIC int
  1703. trio_string_match_case
  1704. TRIO_ARGS2((self, other),
  1705. trio_string_t *self,
  1706. trio_string_t *other)
  1707. {
  1708. assert(self);
  1709. assert(other);
  1710. return trio_match_case(self->content, other->content);
  1711. }
  1712. #endif /* !defined(TRIO_MINIMAL) */
  1713. #if !defined(TRIO_MINIMAL)
  1714. /*
  1715. * trio_xstring_match_case
  1716. */
  1717. TRIO_STRING_PUBLIC int
  1718. trio_xstring_match_case
  1719. TRIO_ARGS2((self, other),
  1720. trio_string_t *self,
  1721. TRIO_CONST char *other)
  1722. {
  1723. assert(self);
  1724. assert(other);
  1725. return trio_match_case(self->content, other);
  1726. }
  1727. #endif /* !defined(TRIO_MINIMAL) */
  1728. #if !defined(TRIO_MINIMAL)
  1729. /*
  1730. * trio_string_substring
  1731. */
  1732. TRIO_STRING_PUBLIC char *
  1733. trio_string_substring
  1734. TRIO_ARGS2((self, other),
  1735. trio_string_t *self,
  1736. trio_string_t *other)
  1737. {
  1738. assert(self);
  1739. assert(other);
  1740. return trio_substring(self->content, other->content);
  1741. }
  1742. #endif /* !defined(TRIO_MINIMAL) */
  1743. #if !defined(TRIO_MINIMAL)
  1744. /*
  1745. * trio_xstring_substring
  1746. */
  1747. TRIO_STRING_PUBLIC char *
  1748. trio_xstring_substring
  1749. TRIO_ARGS2((self, other),
  1750. trio_string_t *self,
  1751. TRIO_CONST char *other)
  1752. {
  1753. assert(self);
  1754. assert(other);
  1755. return trio_substring(self->content, other);
  1756. }
  1757. #endif /* !defined(TRIO_MINIMAL) */
  1758. #if !defined(TRIO_MINIMAL)
  1759. /*
  1760. * trio_string_upper
  1761. */
  1762. TRIO_STRING_PUBLIC int
  1763. trio_string_upper
  1764. TRIO_ARGS1((self),
  1765. trio_string_t *self)
  1766. {
  1767. assert(self);
  1768. return trio_upper(self->content);
  1769. }
  1770. #endif /* !defined(TRIO_MINIMAL) */
  1771. /** @} End of DynamicStrings */