string.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_STRING_H__
  20. #define __PJ_STRING_H__
  21. /**
  22. * @file string.h
  23. * @brief PJLIB String Operations.
  24. */
  25. #include <pj/types.h>
  26. #include <pj/compat/string.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJ_PSTR String Operations
  30. * @ingroup PJ_DS
  31. * @{
  32. * This module provides string manipulation API.
  33. *
  34. * \section pj_pstr_not_null_sec PJLIB String is NOT Null Terminated!
  35. *
  36. * That is the first information that developers need to know. Instead
  37. * of using normal C string, strings in PJLIB are represented as
  38. * pj_str_t structure below:
  39. *
  40. * <pre>
  41. * typedef struct pj_str_t
  42. * {
  43. * char *ptr;
  44. * pj_ssize_t slen;
  45. * } pj_str_t;
  46. * </pre>
  47. *
  48. * There are some advantages of using this approach:
  49. * - the string can point to arbitrary location in memory even
  50. * if the string in that location is not null terminated. This is
  51. * most usefull for text parsing, where the parsed text can just
  52. * point to the original text in the input. If we use C string,
  53. * then we will have to copy the text portion from the input
  54. * to a string variable.
  55. * - because the length of the string is known, string copy operation
  56. * can be made more efficient.
  57. *
  58. * Most of APIs in PJLIB that expect or return string will represent
  59. * the string as pj_str_t instead of normal C string.
  60. *
  61. * \section pj_pstr_examples_sec Examples
  62. *
  63. * For some examples, please see:
  64. * - String test: \src{pjlib/src/pjlib-test/string.c}
  65. */
  66. /**
  67. * Check if a string is truncated and if yes, put a suffix of ".."
  68. * to indicate the truncation.
  69. * This macro is used to check the result of pj_ansi_snprintf().
  70. *
  71. * @param ret The return value of pj_ansi_snprintf().
  72. * @param str The string.
  73. * @param len The length of the string buffer.
  74. */
  75. #define PJ_CHECK_TRUNC_STR(ret, str, len) \
  76. if ((int)(ret) >= (int)(len) || (ret) < 0) pj_ansi_strxcpy((str) + (len) - 3, "..", 3)
  77. /**
  78. * Create string initializer from a normal C string.
  79. *
  80. * @param str Null terminated string to be stored.
  81. *
  82. * @return pj_str_t.
  83. */
  84. PJ_IDECL(pj_str_t) pj_str(char *str);
  85. /**
  86. * Create constant string from normal C string.
  87. *
  88. * @param str The string to be initialized.
  89. * @param s Null terminated string.
  90. *
  91. * @return pj_str_t.
  92. */
  93. PJ_INLINE(const pj_str_t*) pj_cstr(pj_str_t *str, const char *s)
  94. {
  95. str->ptr = (char*)s;
  96. str->slen = s ? (pj_ssize_t)strlen(s) : 0;
  97. return str;
  98. }
  99. /**
  100. * Set the pointer and length to the specified value.
  101. *
  102. * @param str the string.
  103. * @param ptr pointer to set.
  104. * @param length length to set.
  105. *
  106. * @return the string.
  107. */
  108. PJ_INLINE(pj_str_t*) pj_strset( pj_str_t *str, char *ptr, pj_size_t length)
  109. {
  110. str->ptr = ptr;
  111. str->slen = (pj_ssize_t)length;
  112. return str;
  113. }
  114. /**
  115. * Set the pointer and length of the string to the source string, which
  116. * must be NULL terminated.
  117. *
  118. * @param str the string.
  119. * @param src pointer to set.
  120. *
  121. * @return the string.
  122. */
  123. PJ_INLINE(pj_str_t*) pj_strset2( pj_str_t *str, char *src)
  124. {
  125. str->ptr = src;
  126. str->slen = src ? (pj_ssize_t)strlen(src) : 0;
  127. return str;
  128. }
  129. /**
  130. * Set the pointer and the length of the string.
  131. *
  132. * @param str The target string.
  133. * @param begin The start of the string.
  134. * @param end The end of the string.
  135. *
  136. * @return the target string.
  137. */
  138. PJ_INLINE(pj_str_t*) pj_strset3( pj_str_t *str, char *begin, char *end )
  139. {
  140. str->ptr = begin;
  141. str->slen = (pj_ssize_t)(end-begin);
  142. return str;
  143. }
  144. /**
  145. * Assign string.
  146. *
  147. * @param dst The target string.
  148. * @param src The source string.
  149. *
  150. * @return the target string.
  151. */
  152. PJ_IDECL(pj_str_t*) pj_strassign( pj_str_t *dst, pj_str_t *src );
  153. /**
  154. * Copy string contents.
  155. *
  156. * @param dst The target string.
  157. * @param src The source string.
  158. *
  159. * @return the target string.
  160. */
  161. PJ_IDECL(pj_str_t*) pj_strcpy(pj_str_t *dst, const pj_str_t *src);
  162. /**
  163. * Copy string contents.
  164. *
  165. * @param dst The target string.
  166. * @param src The source string.
  167. *
  168. * @return the target string.
  169. */
  170. PJ_IDECL(pj_str_t*) pj_strcpy2(pj_str_t *dst, const char *src);
  171. /**
  172. * Copy source string to destination up to the specified max length.
  173. *
  174. * @param dst The target string.
  175. * @param src The source string.
  176. * @param max Maximum characters to copy.
  177. *
  178. * @return the target string.
  179. */
  180. PJ_IDECL(pj_str_t*) pj_strncpy(pj_str_t *dst, const pj_str_t *src,
  181. pj_ssize_t max);
  182. /**
  183. * Copy source string to destination up to the specified max length,
  184. * and NULL terminate the destination. If source string length is
  185. * greater than or equal to max, then max-1 will be copied.
  186. *
  187. * @param dst The target string.
  188. * @param src The source string.
  189. * @param max Maximum characters to copy.
  190. *
  191. * @return the target string.
  192. */
  193. PJ_IDECL(pj_str_t*) pj_strncpy_with_null(pj_str_t *dst, const pj_str_t *src,
  194. pj_ssize_t max);
  195. /**
  196. * Duplicate string.
  197. *
  198. * @param pool The pool.
  199. * @param dst The string result.
  200. * @param src The string to duplicate.
  201. *
  202. * @return the string result.
  203. */
  204. PJ_IDECL(pj_str_t*) pj_strdup(pj_pool_t *pool,
  205. pj_str_t *dst,
  206. const pj_str_t *src);
  207. /**
  208. * Duplicate string and NULL terminate the destination string.
  209. *
  210. * @param pool The pool.
  211. * @param dst The string result.
  212. * @param src The string to duplicate.
  213. *
  214. * @return The string result.
  215. */
  216. PJ_IDECL(pj_str_t*) pj_strdup_with_null(pj_pool_t *pool,
  217. pj_str_t *dst,
  218. const pj_str_t *src);
  219. /**
  220. * Duplicate string.
  221. *
  222. * @param pool The pool.
  223. * @param dst The string result.
  224. * @param src The string to duplicate.
  225. *
  226. * @return the string result.
  227. */
  228. PJ_IDECL(pj_str_t*) pj_strdup2(pj_pool_t *pool,
  229. pj_str_t *dst,
  230. const char *src);
  231. /**
  232. * Duplicate string and NULL terminate the destination string.
  233. *
  234. * @param pool The pool.
  235. * @param dst The string result.
  236. * @param src The string to duplicate.
  237. *
  238. * @return The string result.
  239. */
  240. PJ_IDECL(pj_str_t*) pj_strdup2_with_null(pj_pool_t *pool,
  241. pj_str_t *dst,
  242. const char *src);
  243. /**
  244. * Duplicate string.
  245. *
  246. * @param pool The pool.
  247. * @param src The string to duplicate.
  248. *
  249. * @return the string result.
  250. */
  251. PJ_IDECL(pj_str_t) pj_strdup3(pj_pool_t *pool, const char *src);
  252. /**
  253. * Return the length of the string.
  254. *
  255. * @param str The string.
  256. *
  257. * @return the length of the string.
  258. */
  259. PJ_INLINE(pj_size_t) pj_strlen( const pj_str_t *str )
  260. {
  261. return str->slen;
  262. }
  263. /**
  264. * Return the pointer to the string data.
  265. *
  266. * @param str The string.
  267. *
  268. * @return the pointer to the string buffer.
  269. */
  270. PJ_INLINE(const char*) pj_strbuf( const pj_str_t *str )
  271. {
  272. return str->ptr;
  273. }
  274. /**
  275. * Compare strings.
  276. *
  277. * @param str1 The string to compare.
  278. * @param str2 The string to compare.
  279. *
  280. * @return
  281. * - < 0 if str1 is less than str2
  282. * - 0 if str1 is identical to str2
  283. * - > 0 if str1 is greater than str2
  284. */
  285. PJ_IDECL(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2);
  286. /**
  287. * Compare strings.
  288. *
  289. * @param str1 The string to compare.
  290. * @param str2 The string to compare.
  291. *
  292. * @return
  293. * - < 0 if str1 is less than str2
  294. * - 0 if str1 is identical to str2
  295. * - > 0 if str1 is greater than str2
  296. */
  297. PJ_IDECL(int) pj_strcmp2( const pj_str_t *str1, const char *str2 );
  298. /**
  299. * Compare strings.
  300. *
  301. * @param str1 The string to compare.
  302. * @param str2 The string to compare.
  303. * @param len The maximum number of characters to compare.
  304. *
  305. * @return
  306. * - < 0 if str1 is less than str2
  307. * - 0 if str1 is identical to str2
  308. * - > 0 if str1 is greater than str2
  309. */
  310. PJ_IDECL(int) pj_strncmp( const pj_str_t *str1, const pj_str_t *str2,
  311. pj_size_t len);
  312. /**
  313. * Compare strings.
  314. *
  315. * @param str1 The string to compare.
  316. * @param str2 The string to compare.
  317. * @param len The maximum number of characters to compare.
  318. *
  319. * @return
  320. * - < 0 if str1 is less than str2
  321. * - 0 if str1 is identical to str2
  322. * - > 0 if str1 is greater than str2
  323. */
  324. PJ_IDECL(int) pj_strncmp2( const pj_str_t *str1, const char *str2,
  325. pj_size_t len);
  326. /**
  327. * Perform case-insensitive comparison to the strings.
  328. *
  329. * @param str1 The string to compare.
  330. * @param str2 The string to compare.
  331. *
  332. * @return
  333. * - < 0 if str1 is less than str2
  334. * - 0 if str1 is equal to str2
  335. * - > 0 if str1 is greater than str2
  336. */
  337. PJ_IDECL(int) pj_stricmp(const pj_str_t *str1, const pj_str_t *str2);
  338. /**
  339. * Perform lowercase comparison to the strings which consists of only
  340. * alnum characters. More over, it will only return non-zero if both
  341. * strings are not equal, not the usual negative or positive value.
  342. *
  343. * If non-alnum inputs are given, then the function may mistakenly
  344. * treat two strings as equal.
  345. *
  346. * @param str1 The string to compare.
  347. * @param str2 The string to compare.
  348. * @param len The length to compare.
  349. *
  350. * @return
  351. * - 0 if str1 is equal to str2
  352. * - (-1) if not equal.
  353. */
  354. #if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0
  355. PJ_IDECL(int) strnicmp_alnum(const char *str1, const char *str2,
  356. int len);
  357. #else
  358. #define strnicmp_alnum pj_ansi_strnicmp
  359. #endif
  360. /**
  361. * Perform lowercase comparison to the strings which consists of only
  362. * alnum characters. More over, it will only return non-zero if both
  363. * strings are not equal, not the usual negative or positive value.
  364. *
  365. * If non-alnum inputs are given, then the function may mistakenly
  366. * treat two strings as equal.
  367. *
  368. * @param str1 The string to compare.
  369. * @param str2 The string to compare.
  370. *
  371. * @return
  372. * - 0 if str1 is equal to str2
  373. * - (-1) if not equal.
  374. */
  375. #if defined(PJ_HAS_STRICMP_ALNUM) && PJ_HAS_STRICMP_ALNUM!=0
  376. PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2);
  377. #else
  378. #define pj_stricmp_alnum pj_stricmp
  379. #endif
  380. /**
  381. * Perform case-insensitive comparison to the strings.
  382. *
  383. * @param str1 The string to compare.
  384. * @param str2 The string to compare.
  385. *
  386. * @return
  387. * - < 0 if str1 is less than str2
  388. * - 0 if str1 is identical to str2
  389. * - > 0 if str1 is greater than str2
  390. */
  391. PJ_IDECL(int) pj_stricmp2( const pj_str_t *str1, const char *str2);
  392. /**
  393. * Perform case-insensitive comparison to the strings.
  394. *
  395. * @param str1 The string to compare.
  396. * @param str2 The string to compare.
  397. * @param len The maximum number of characters to compare.
  398. *
  399. * @return
  400. * - < 0 if str1 is less than str2
  401. * - 0 if str1 is identical to str2
  402. * - > 0 if str1 is greater than str2
  403. */
  404. PJ_IDECL(int) pj_strnicmp( const pj_str_t *str1, const pj_str_t *str2,
  405. pj_size_t len);
  406. /**
  407. * Perform case-insensitive comparison to the strings.
  408. *
  409. * @param str1 The string to compare.
  410. * @param str2 The string to compare.
  411. * @param len The maximum number of characters to compare.
  412. *
  413. * @return
  414. * - < 0 if str1 is less than str2
  415. * - 0 if str1 is identical to str2
  416. * - > 0 if str1 is greater than str2
  417. */
  418. PJ_IDECL(int) pj_strnicmp2( const pj_str_t *str1, const char *str2,
  419. pj_size_t len);
  420. /**
  421. * Concatenate strings.
  422. *
  423. * @param dst The destination string.
  424. * @param src The source string.
  425. */
  426. PJ_IDECL(void) pj_strcat(pj_str_t *dst, const pj_str_t *src);
  427. /**
  428. * Concatenate strings.
  429. *
  430. * @param dst The destination string.
  431. * @param src The source string.
  432. */
  433. PJ_IDECL(void) pj_strcat2(pj_str_t *dst, const char *src);
  434. /**
  435. * Finds a character in a string.
  436. *
  437. * @param str The string.
  438. * @param chr The character to find.
  439. *
  440. * @return the pointer to first character found, or NULL.
  441. */
  442. PJ_INLINE(char*) pj_strchr( const pj_str_t *str, int chr)
  443. {
  444. if (str->slen == 0)
  445. return NULL;
  446. return (char*) memchr((char*)str->ptr, chr, str->slen);
  447. }
  448. /**
  449. * Find the first index of character, in a string, that does not belong to a
  450. * set of characters.
  451. *
  452. * @param str The string.
  453. * @param set_char The string containing the set of characters.
  454. *
  455. * @return the index of the first character in the str that doesn't belong to
  456. * set_char. If str starts with a character not in set_char, return 0.
  457. */
  458. PJ_DECL(pj_ssize_t) pj_strspn(const pj_str_t *str, const pj_str_t *set_char);
  459. /**
  460. * Find the first index of character, in a string, that does not belong to a
  461. * set of characters.
  462. *
  463. * @param str The string.
  464. * @param set_char The string containing the set of characters.
  465. *
  466. * @return the index of the first character in the str that doesn't belong to
  467. * set_char. If str starts with a character not in set_char, return 0.
  468. */
  469. PJ_DECL(pj_ssize_t) pj_strspn2(const pj_str_t *str, const char *set_char);
  470. /**
  471. * Find the first index of character, in a string, that belong to a set of
  472. * characters.
  473. *
  474. * @param str The string.
  475. * @param set_char The string containing the set of characters.
  476. *
  477. * @return the index of the first character in the str that belong to
  478. * set_char. If no match is found, return the length of str.
  479. */
  480. PJ_DECL(pj_ssize_t) pj_strcspn(const pj_str_t *str, const pj_str_t *set_char);
  481. /**
  482. * Find the first index of character, in a string, that belong to a set of
  483. * characters.
  484. *
  485. * @param str The string.
  486. * @param set_char The string containing the set of characters.
  487. *
  488. * @return the index of the first character in the str that belong to
  489. * set_char. If no match is found, return the length of str.
  490. */
  491. PJ_DECL(pj_ssize_t) pj_strcspn2(const pj_str_t *str, const char *set_char);
  492. /**
  493. * Find tokens from a string using the delimiter.
  494. *
  495. * @param str The string.
  496. * @param delim The string containing the delimiter. It might contain
  497. * multiple character treated as unique set. If same character
  498. * was found on the set, it will be skipped.
  499. * @param tok The string containing the token.
  500. * @param start_idx The search will start from this index.
  501. *
  502. * @return the index of token from the str, or the length of the str
  503. * if the token is not found.
  504. */
  505. PJ_DECL(pj_ssize_t) pj_strtok(const pj_str_t *str, const pj_str_t *delim,
  506. pj_str_t *tok, pj_size_t start_idx);
  507. /**
  508. * Find tokens from a string using the delimiter.
  509. *
  510. * @param str The string.
  511. * @param delim The string containing the delimiter. It might contain
  512. * multiple character treated as unique set. If same character
  513. * was found on the set, it will be skipped.
  514. * @param tok The string containing the token.
  515. * @param start_idx The search will start from this index.
  516. *
  517. * @return the index of token from the str, or the length of the str
  518. * if the token is not found.
  519. */
  520. PJ_DECL(pj_ssize_t) pj_strtok2(const pj_str_t *str, const char *delim,
  521. pj_str_t *tok, pj_size_t start_idx);
  522. /**
  523. * Find the occurence of a substring substr in string str.
  524. *
  525. * @param str The string to search.
  526. * @param substr The string to search fo.
  527. *
  528. * @return the pointer to the position of substr in str, or NULL. Note
  529. * that if str is not NULL terminated, the returned pointer
  530. * is pointing to non-NULL terminated string.
  531. */
  532. PJ_DECL(char*) pj_strstr(const pj_str_t *str, const pj_str_t *substr);
  533. /**
  534. * Performs substring lookup like pj_strstr() but ignores the case of
  535. * both strings.
  536. *
  537. * @param str The string to search.
  538. * @param substr The string to search fo.
  539. *
  540. * @return the pointer to the position of substr in str, or NULL. Note
  541. * that if str is not NULL terminated, the returned pointer
  542. * is pointing to non-NULL terminated string.
  543. */
  544. PJ_DECL(char*) pj_stristr(const pj_str_t *str, const pj_str_t *substr);
  545. /**
  546. * Remove (trim) leading whitespaces from the string.
  547. *
  548. * @param str The string.
  549. *
  550. * @return the string.
  551. */
  552. PJ_DECL(pj_str_t*) pj_strltrim( pj_str_t *str );
  553. /**
  554. * Remove (trim) the trailing whitespaces from the string.
  555. *
  556. * @param str The string.
  557. *
  558. * @return the string.
  559. */
  560. PJ_DECL(pj_str_t*) pj_strrtrim( pj_str_t *str );
  561. /**
  562. * Remove (trim) leading and trailing whitespaces from the string.
  563. *
  564. * @param str The string.
  565. *
  566. * @return the string.
  567. */
  568. PJ_IDECL(pj_str_t*) pj_strtrim( pj_str_t *str );
  569. /**
  570. * Initialize the buffer with some random string. Note that the
  571. * generated string is not NULL terminated.
  572. *
  573. * @param str the string to store the result.
  574. * @param length the length of the random string to generate.
  575. *
  576. * @return the string.
  577. */
  578. PJ_DECL(char*) pj_create_random_string(char *str, pj_size_t length);
  579. /**
  580. * Convert string to signed integer. The conversion will stop as
  581. * soon as non-digit character is found or all the characters have
  582. * been processed.
  583. *
  584. * @param str the string.
  585. *
  586. * @return the integer.
  587. */
  588. PJ_DECL(long) pj_strtol(const pj_str_t *str);
  589. /**
  590. * Convert string to signed long integer. The conversion will stop as
  591. * soon as non-digit character is found or all the characters have
  592. * been processed.
  593. *
  594. * @param str the string.
  595. * @param value Pointer to a long to receive the value.
  596. *
  597. * @return PJ_SUCCESS if successful. Otherwise:
  598. * PJ_ETOOSMALL if the value was an impossibly long negative number.
  599. * In this case *value will be set to LONG_MIN.
  600. * \n
  601. * PJ_ETOOBIG if the value was an impossibly long positive number.
  602. * In this case, *value will be set to LONG_MAX.
  603. * \n
  604. * PJ_EINVAL if the input string was NULL, the value pointer was NULL
  605. * or the input string could not be parsed at all such as starting with
  606. * a character other than a '+', '-' or not in the '0' - '9' range.
  607. * In this case, *value will be left untouched.
  608. */
  609. PJ_DECL(pj_status_t) pj_strtol2(const pj_str_t *str, long *value);
  610. /**
  611. * Convert string to unsigned integer. The conversion will stop as
  612. * soon as non-digit character is found or all the characters have
  613. * been processed.
  614. *
  615. * @param str the string.
  616. *
  617. * @return the unsigned integer.
  618. */
  619. PJ_DECL(unsigned long) pj_strtoul(const pj_str_t *str);
  620. /**
  621. * Convert strings to an unsigned long-integer value.
  622. * This function stops reading the string input either when the number
  623. * of characters has exceeded the length of the input or it has read
  624. * the first character it cannot recognize as part of a number, that is
  625. * a character greater than or equal to base.
  626. *
  627. * @param str The input string.
  628. * @param endptr Optional pointer to receive the remainder/unparsed
  629. * portion of the input.
  630. * @param base Number base to use.
  631. *
  632. * @return the unsigned integer number.
  633. */
  634. PJ_DECL(unsigned long) pj_strtoul2(const pj_str_t *str, pj_str_t *endptr,
  635. unsigned base);
  636. /**
  637. * Convert string to unsigned long integer. The conversion will stop as
  638. * soon as non-digit character is found or all the characters have
  639. * been processed.
  640. *
  641. * @param str The input string.
  642. * @param value Pointer to an unsigned long to receive the value.
  643. * @param base Number base to use.
  644. *
  645. * @return PJ_SUCCESS if successful. Otherwise:
  646. * PJ_ETOOBIG if the value was an impossibly long positive number.
  647. * In this case, *value will be set to ULONG_MAX.
  648. * \n
  649. * PJ_EINVAL if the input string was NULL, the value pointer was NULL
  650. * or the input string could not be parsed at all such as starting
  651. * with a character outside the base character range. In this case,
  652. * *value will be left untouched.
  653. */
  654. PJ_DECL(pj_status_t) pj_strtoul3(const pj_str_t *str, unsigned long *value,
  655. unsigned base);
  656. /**
  657. * Convert string to generic unsigned integer. The conversion will stop as
  658. * soon as non-digit character is found or all the characters have
  659. * been processed.
  660. *
  661. * @param str The input string.
  662. * @param value Pointer to an unsigned integer to receive the value.
  663. * The value will be a 64 bit unsigned integer if the system
  664. * supports it, otherwise a 32 bit unsigned integer.
  665. * @param base Number base to use.
  666. *
  667. * @return PJ_SUCCESS if successful. Otherwise:
  668. * PJ_ETOOBIG if the value was an impossibly long positive number.
  669. * In this case, *value will be set to ULLONG_MAX (for 64 bit) or
  670. * ULONG_MAX (for 32 bit).
  671. * \n
  672. * PJ_EINVAL if the input string was NULL, the value pointer was NULL
  673. * or the input string could not be parsed at all such as starting
  674. * with a character outside the base character range. In this case,
  675. * *value will be left untouched.
  676. */
  677. PJ_DECL(pj_status_t) pj_strtoul4(const pj_str_t *str, pj_uint_t *value,
  678. unsigned base);
  679. /**
  680. * Convert string to float.
  681. *
  682. * @param str the string.
  683. *
  684. * @return the value.
  685. */
  686. PJ_DECL(float) pj_strtof(const pj_str_t *str);
  687. /**
  688. * Utility to convert unsigned integer to string. Note that the
  689. * string will be NULL terminated.
  690. *
  691. * @param val the unsigned integer value.
  692. * @param buf the buffer
  693. *
  694. * @return the number of characters written
  695. */
  696. PJ_DECL(int) pj_utoa(unsigned long val, char *buf);
  697. /**
  698. * Utility to convert generic unsigned integer to string. Note that the
  699. * string will be NULL terminated.
  700. *
  701. * This function will take 64 bit unsigned integer if the system has one,
  702. * otherwise it takes 32 bit unsigned integer.
  703. *
  704. * @param val the unsigned integer value.
  705. * @param buf the buffer
  706. *
  707. * @return the number of characters written
  708. */
  709. PJ_DECL(int) pj_utoa2(pj_uint_t val, char *buf);
  710. /**
  711. * Convert unsigned integer to string with minimum digits. Note that the
  712. * string will be NULL terminated.
  713. *
  714. * @param val The unsigned integer value.
  715. * @param buf The buffer.
  716. * @param min_dig Minimum digits to be printed, or zero to specify no
  717. * minimum digit.
  718. * @param pad The padding character to be put in front of the string
  719. * when the digits is less than minimum.
  720. *
  721. * @return the number of characters written.
  722. */
  723. PJ_DECL(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad);
  724. /**
  725. * Convert generic unsigned integer to string with minimum digits. Note that
  726. * the string will be NULL terminated.
  727. *
  728. * This function will take 64 bit unsigned integer if the system has one,
  729. * otherwise it takes 32 bit unsigned integer.
  730. *
  731. * @param val The unsigned integer value.
  732. * @param buf The buffer.
  733. * @param min_dig Minimum digits to be printed, or zero to specify no
  734. * minimum digit.
  735. * @param pad The padding character to be put in front of the string
  736. * when the digits is less than minimum.
  737. *
  738. * @return the number of characters written.
  739. */
  740. PJ_DECL(int) pj_utoa_pad2( pj_uint_t val, char *buf, int min_dig, int pad);
  741. /**
  742. * Fill the memory location with zero.
  743. *
  744. * @param dst The destination buffer.
  745. * @param size The number of bytes.
  746. */
  747. PJ_INLINE(void) pj_bzero(void *dst, pj_size_t size)
  748. {
  749. #if defined(PJ_HAS_BZERO) && PJ_HAS_BZERO!=0
  750. bzero(dst, size);
  751. #else
  752. memset(dst, 0, size);
  753. #endif
  754. }
  755. /**
  756. * Fill the memory location with value.
  757. *
  758. * @param dst The destination buffer.
  759. * @param c Character to set.
  760. * @param size The number of characters.
  761. *
  762. * @return the value of dst.
  763. */
  764. PJ_INLINE(void*) pj_memset(void *dst, int c, pj_size_t size)
  765. {
  766. return memset(dst, c, size);
  767. }
  768. /**
  769. * Copy buffer.
  770. *
  771. * @param dst The destination buffer.
  772. * @param src The source buffer.
  773. * @param size The size to copy.
  774. *
  775. * @return the destination buffer.
  776. */
  777. PJ_INLINE(void*) pj_memcpy(void *dst, const void *src, pj_size_t size)
  778. {
  779. return memcpy(dst, src, size);
  780. }
  781. /**
  782. * Move memory.
  783. *
  784. * @param dst The destination buffer.
  785. * @param src The source buffer.
  786. * @param size The size to copy.
  787. *
  788. * @return the destination buffer.
  789. */
  790. PJ_INLINE(void*) pj_memmove(void *dst, const void *src, pj_size_t size)
  791. {
  792. return memmove(dst, src, size);
  793. }
  794. /**
  795. * Compare buffers.
  796. *
  797. * @param buf1 The first buffer.
  798. * @param buf2 The second buffer.
  799. * @param size The size to compare.
  800. *
  801. * @return negative, zero, or positive value.
  802. */
  803. PJ_INLINE(int) pj_memcmp(const void *buf1, const void *buf2, pj_size_t size)
  804. {
  805. return memcmp(buf1, buf2, size);
  806. }
  807. /**
  808. * Find character in the buffer.
  809. *
  810. * @param buf The buffer.
  811. * @param c The character to find.
  812. * @param size The size to check.
  813. *
  814. * @return the pointer to location where the character is found, or NULL if
  815. * not found.
  816. */
  817. PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size)
  818. {
  819. return (void*)memchr((void*)buf, c, size);
  820. }
  821. /**
  822. * Copy the string, or as much of it as fits, into the dest buffer.
  823. * Regardless of whether all characters were copied, the destination
  824. * buffer will be null terminated, unless dst_size is zero which in
  825. * this case nothing will be written to dst and the function will
  826. * return -PJ_ETOOBIG.
  827. *
  828. * @param dst The destination string.
  829. * @param src The source string.
  830. * @param dst_size The full size of the destination string buffer.
  831. *
  832. * @return The number of characters copied (not including the trailing NUL) or
  833. * -PJ_ETOOBIG if the destination buffer wasn't big enough,
  834. * -PJ_EINVAL if the dst or src is NULL.
  835. */
  836. PJ_DECL(int) pj_ansi_strxcpy(char *dst, const char *src, pj_size_t dst_size);
  837. /**
  838. * Same as pj_ansi_strxcpy() but takes pj_str_t as the source.
  839. * If src contains null character, copying will stop at the first null
  840. * character in src.
  841. *
  842. * @param dst The destination string.
  843. * @param src The source string.
  844. * @param dst_size The full size of the destination string buffer.
  845. *
  846. * @return The number of characters copied (not including the trailing NUL) or
  847. * -PJ_ETOOBIG if the destination buffer wasn't big enough,
  848. * -PJ_EINVAL if the dst or src is NULL.
  849. */
  850. PJ_DECL(int) pj_ansi_strxcpy2(char *dst, const pj_str_t *src,
  851. pj_size_t dst_size);
  852. /**
  853. * Concatenate src, or as much of it as fits, into the dest buffer.
  854. * Regardless of whether all characters were copied, the destination
  855. * buffer will be null terminated, unless dst_size is zero which in
  856. * this case nothing will be written to dst and the function will
  857. * return -PJ_ETOOBIG.
  858. *
  859. * @param dst The destination string.
  860. * @param src The source string.
  861. * @param dst_size The full size of the destination string buffer.
  862. *
  863. * @return Final length of dst string (not including the trailing NUL) or
  864. * -PJ_ETOOBIG if the destination buffer wasn't big enough,
  865. * -PJ_EINVAL if the dst or src is NULL.
  866. */
  867. PJ_DECL(int) pj_ansi_strxcat(char *dst, const char *src, pj_size_t dst_size);
  868. /**
  869. * @}
  870. */
  871. #if PJ_FUNCTIONS_ARE_INLINED
  872. # include <pj/string_i.h>
  873. #endif
  874. PJ_END_DECL
  875. #endif /* __PJ_STRING_H__ */