plstr.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef _plstr_h
  6. #define _plstr_h
  7. /*
  8. * plstr.h
  9. *
  10. * This header file exports the API to the NSPR portable library or string-
  11. * handling functions.
  12. *
  13. * This API was not designed as an "optimal" or "ideal" string library; it
  14. * was based on the good ol' unix string.3 functions, and was written to
  15. *
  16. * 1) replace the libc functions, for cross-platform consistency,
  17. * 2) complete the API on platforms lacking common functions (e.g.,
  18. * strcase*), and
  19. * 3) to implement some obvious "closure" functions that I've seen
  20. * people hacking around in our code.
  21. *
  22. * Point number three largely means that most functions have an "strn"
  23. * limited-length version, and all comparison routines have a non-case-
  24. * sensitive version available.
  25. */
  26. #include "prtypes.h"
  27. PR_BEGIN_EXTERN_C
  28. /*
  29. * PL_strlen
  30. *
  31. * Returns the length of the provided string, not including the trailing '\0'.
  32. */
  33. PR_EXTERN(PRUint32)
  34. PL_strlen(const char *str);
  35. /*
  36. * PL_strnlen
  37. *
  38. * Returns the length of the provided string, not including the trailing '\0',
  39. * up to the indicated maximum. The string will not be examined beyond the
  40. * maximum; if no terminating '\0' is found, the maximum will be returned.
  41. */
  42. PR_EXTERN(PRUint32)
  43. PL_strnlen(const char *str, PRUint32 max);
  44. /*
  45. * PL_strcpy
  46. *
  47. * Copies the source string, up to and including the trailing '\0', into the
  48. * destination buffer. It does not (can not) verify that the destination
  49. * buffer is large enough. It returns the "dest" argument.
  50. */
  51. PR_EXTERN(char *)
  52. PL_strcpy(char *dest, const char *src);
  53. /*
  54. * PL_strncpy
  55. *
  56. * Copies the source string into the destination buffer, up to and including
  57. * the trailing '\0' or up to and including the max'th character, whichever
  58. * comes first. It does not (can not) verify that the destination buffer is
  59. * large enough. If the source string is longer than the maximum length,
  60. * the result will *not* be null-terminated (JLRU).
  61. */
  62. PR_EXTERN(char *)
  63. PL_strncpy(char *dest, const char *src, PRUint32 max);
  64. /*
  65. * PL_strncpyz
  66. *
  67. * Copies the source string into the destination buffer, up to and including
  68. * the trailing '\0' or up but not including the max'th character, whichever
  69. * comes first. It does not (can not) verify that the destination buffer is
  70. * large enough. The destination string is always terminated with a '\0',
  71. * unlike the traditional libc implementation. It returns the "dest" argument.
  72. *
  73. * NOTE: If you call this with a source "abcdefg" and a max of 5, the
  74. * destination will end up with "abcd\0" (i.e., its strlen length will be 4)!
  75. *
  76. * This means you can do this:
  77. *
  78. * char buffer[ SOME_SIZE ];
  79. * PL_strncpyz(buffer, src, sizeof(buffer));
  80. *
  81. * and the result will be properly terminated.
  82. */
  83. PR_EXTERN(char *)
  84. PL_strncpyz(char *dest, const char *src, PRUint32 max);
  85. /*
  86. * PL_strdup
  87. *
  88. * Returns a pointer to a malloc'd extent of memory containing a duplicate
  89. * of the argument string. The size of the allocated extent is one greater
  90. * than the length of the argument string, because of the terminator. A
  91. * null argument, like a zero-length argument, will result in a pointer to
  92. * a one-byte extent containing the null value. This routine returns null
  93. * upon malloc failure.
  94. */
  95. PR_EXTERN(char *)
  96. PL_strdup(const char *s);
  97. /*
  98. * PL_strfree
  99. *
  100. * Free memory allocated by PL_strdup
  101. */
  102. PR_EXTERN(void)
  103. PL_strfree(char *s);
  104. /*
  105. * PL_strndup
  106. *
  107. * Returns a pointer to a malloc'd extent of memory containing a duplicate
  108. * of the argument string, up to the maximum specified. If the argument
  109. * string has a length greater than the value of the specified maximum, the
  110. * return value will be a pointer to an extent of memory of length one
  111. * greater than the maximum specified. A null string, a zero-length string,
  112. * or a zero maximum will all result in a pointer to a one-byte extent
  113. * containing the null value. This routine returns null upon malloc failure.
  114. */
  115. PR_EXTERN(char *)
  116. PL_strndup(const char *s, PRUint32 max);
  117. /*
  118. * PL_strcat
  119. *
  120. * Appends a copy of the string pointed to by the second argument to the
  121. * end of the string pointed to by the first. The destination buffer is
  122. * not (can not be) checked for sufficient size. A null destination
  123. * argument returns null; otherwise, the first argument is returned.
  124. */
  125. PR_EXTERN(char *)
  126. PL_strcat(char *dst, const char *src);
  127. /*
  128. * PL_strncat
  129. *
  130. * Appends a copy of the string pointed to by the second argument, up to
  131. * the maximum size specified, to the end of the string pointed to by the
  132. * first. The destination buffer is not (can not be) checked for sufficient
  133. * size. A null destination argument returns null; otherwise, the first
  134. * argument is returned. If the maximum size limits the copy, then the
  135. * result will *not* be null-terminated (JLRU). A null destination
  136. * returns null; otherwise, the destination argument is returned.
  137. */
  138. PR_EXTERN(char *)
  139. PL_strncat(char *dst, const char *src, PRUint32 max);
  140. /*
  141. * PL_strcatn
  142. *
  143. * Appends a copy of the string pointed to by the third argument, to the
  144. * end of the string pointed to by the first. The second argument specifies
  145. * the maximum size of the destination buffer, including the null termination.
  146. * If the existing string in dst is longer than the max, no action is taken.
  147. * The resulting string will be null-terminated. A null destination returns
  148. * null; otherwise, the destination argument is returned.
  149. */
  150. PR_EXTERN(char *)
  151. PL_strcatn(char *dst, PRUint32 max, const char *src);
  152. /*
  153. * PL_strcmp
  154. *
  155. * Returns an integer, the sign of which -- positive, zero, or negative --
  156. * reflects the lexical sorting order of the two strings indicated. The
  157. * result is positive if the first string comes after the second. The
  158. * NSPR implementation is not i18n.
  159. */
  160. PR_EXTERN(PRIntn)
  161. PL_strcmp(const char *a, const char *b);
  162. /*
  163. * PL_strncmp
  164. *
  165. * Returns an integer, the sign of which -- positive, zero, or negative --
  166. * reflects the lexical sorting order of the two strings indicated, up to
  167. * the maximum specified. The result is positive if the first string comes
  168. * after the second. The NSPR implementation is not i18n. If the maximum
  169. * is zero, only the existance or non-existance (pointer is null) of the
  170. * strings is compared.
  171. */
  172. PR_EXTERN(PRIntn)
  173. PL_strncmp(const char *a, const char *b, PRUint32 max);
  174. /*
  175. * PL_strcasecmp
  176. *
  177. * Returns an integer, the sign of which -- positive, zero or negative --
  178. * reflects the case-insensitive lexical sorting order of the two strings
  179. * indicated. The result is positive if the first string comes after the
  180. * second. The NSPR implementation is not i18n.
  181. */
  182. PR_EXTERN(PRIntn)
  183. PL_strcasecmp(const char *a, const char *b);
  184. /*
  185. * PL_strncasecmp
  186. *
  187. * Returns an integer, the sign of which -- positive, zero or negative --
  188. * reflects the case-insensitive lexical sorting order of the first n characters
  189. * of the two strings indicated. The result is positive if the first string comes
  190. * after the second. The NSPR implementation is not i18n.
  191. */
  192. PR_EXTERN(PRIntn)
  193. PL_strncasecmp(const char *a, const char *b, PRUint32 max);
  194. /*
  195. * PL_strchr
  196. *
  197. * Returns a pointer to the first instance of the specified character in the
  198. * provided string. It returns null if the character is not found, or if the
  199. * provided string is null. The character may be the null character.
  200. */
  201. PR_EXTERN(char *)
  202. PL_strchr(const char *s, char c);
  203. /*
  204. * PL_strrchr
  205. *
  206. * Returns a pointer to the last instance of the specified character in the
  207. * provided string. It returns null if the character is not found, or if the
  208. * provided string is null. The character may be the null character.
  209. */
  210. PR_EXTERN(char *)
  211. PL_strrchr(const char *s, char c);
  212. /*
  213. * PL_strnchr
  214. *
  215. * Returns a pointer to the first instance of the specified character within the
  216. * first n characters of the provided string. It returns null if the character
  217. * is not found, or if the provided string is null. The character may be the
  218. * null character.
  219. */
  220. PR_EXTERN(char *)
  221. PL_strnchr(const char *s, char c, PRUint32 n);
  222. /*
  223. * PL_strnrchr
  224. *
  225. * Returns a pointer to the last instance of the specified character within the
  226. * first n characters of the provided string. It returns null if the character is
  227. * not found, or if the provided string is null. The character may be the null
  228. * character.
  229. */
  230. PR_EXTERN(char *)
  231. PL_strnrchr(const char *s, char c, PRUint32 n);
  232. /*
  233. * NOTE: Looking for strcasechr, strcaserchr, strncasechr, or strncaserchr?
  234. * Use strpbrk, strprbrk, strnpbrk or strnprbrk.
  235. */
  236. /*
  237. * PL_strpbrk
  238. *
  239. * Returns a pointer to the first instance in the first string of any character
  240. * (not including the terminating null character) of the second string. It returns
  241. * null if either string is null.
  242. */
  243. PR_EXTERN(char *)
  244. PL_strpbrk(const char *s, const char *list);
  245. /*
  246. * PL_strprbrk
  247. *
  248. * Returns a pointer to the last instance in the first string of any character
  249. * (not including the terminating null character) of the second string. It returns
  250. * null if either string is null.
  251. */
  252. PR_EXTERN(char *)
  253. PL_strprbrk(const char *s, const char *list);
  254. /*
  255. * PL_strnpbrk
  256. *
  257. * Returns a pointer to the first instance (within the first n characters) of any
  258. * character (not including the terminating null character) of the second string.
  259. * It returns null if either string is null.
  260. */
  261. PR_EXTERN(char *)
  262. PL_strnpbrk(const char *s, const char *list, PRUint32 n);
  263. /*
  264. * PL_strnprbrk
  265. *
  266. * Returns a pointer to the last instance (within the first n characters) of any
  267. * character (not including the terminating null character) of the second string.
  268. * It returns null if either string is null.
  269. */
  270. PR_EXTERN(char *)
  271. PL_strnprbrk(const char *s, const char *list, PRUint32 n);
  272. /*
  273. * PL_strstr
  274. *
  275. * Returns a pointer to the first instance of the little string within the
  276. * big one. It returns null if either string is null.
  277. */
  278. PR_EXTERN(char *)
  279. PL_strstr(const char *big, const char *little);
  280. /*
  281. * PL_strrstr
  282. *
  283. * Returns a pointer to the last instance of the little string within the big one.
  284. * It returns null if either string is null.
  285. */
  286. PR_EXTERN(char *)
  287. PL_strrstr(const char *big, const char *little);
  288. /*
  289. * PL_strnstr
  290. *
  291. * Returns a pointer to the first instance of the little string within the first
  292. * n characters of the big one. It returns null if either string is null. It
  293. * returns null if the length of the little string is greater than n.
  294. */
  295. PR_EXTERN(char *)
  296. PL_strnstr(const char *big, const char *little, PRUint32 n);
  297. /*
  298. * PL_strnrstr
  299. *
  300. * Returns a pointer to the last instance of the little string within the first
  301. * n characters of the big one. It returns null if either string is null. It
  302. * returns null if the length of the little string is greater than n.
  303. */
  304. PR_EXTERN(char *)
  305. PL_strnrstr(const char *big, const char *little, PRUint32 max);
  306. /*
  307. * PL_strcasestr
  308. *
  309. * Returns a pointer to the first instance of the little string within the big one,
  310. * ignoring case. It returns null if either string is null.
  311. */
  312. PR_EXTERN(char *)
  313. PL_strcasestr(const char *big, const char *little);
  314. /*
  315. * PL_strcaserstr
  316. *
  317. * Returns a pointer to the last instance of the little string within the big one,
  318. * ignoring case. It returns null if either string is null.
  319. */
  320. PR_EXTERN(char *)
  321. PL_strcaserstr(const char *big, const char *little);
  322. /*
  323. * PL_strncasestr
  324. *
  325. * Returns a pointer to the first instance of the little string within the first
  326. * n characters of the big one, ignoring case. It returns null if either string is
  327. * null. It returns null if the length of the little string is greater than n.
  328. */
  329. PR_EXTERN(char *)
  330. PL_strncasestr(const char *big, const char *little, PRUint32 max);
  331. /*
  332. * PL_strncaserstr
  333. *
  334. * Returns a pointer to the last instance of the little string within the first
  335. * n characters of the big one, ignoring case. It returns null if either string is
  336. * null. It returns null if the length of the little string is greater than n.
  337. */
  338. PR_EXTERN(char *)
  339. PL_strncaserstr(const char *big, const char *little, PRUint32 max);
  340. /*
  341. * PL_strtok_r
  342. *
  343. * Splits the string s1 into tokens, separated by one or more characters
  344. * from the separator string s2. The argument lasts points to a
  345. * user-supplied char * pointer in which PL_strtok_r stores information
  346. * for it to continue scanning the same string.
  347. *
  348. * In the first call to PL_strtok_r, s1 points to a string and the value
  349. * of *lasts is ignored. PL_strtok_r returns a pointer to the first
  350. * token, writes '\0' into the character following the first token, and
  351. * updates *lasts.
  352. *
  353. * In subsequent calls, s1 is null and lasts must stay unchanged from the
  354. * previous call. The separator string s2 may be different from call to
  355. * call. PL_strtok_r returns a pointer to the next token in s1. When no
  356. * token remains in s1, PL_strtok_r returns null.
  357. */
  358. PR_EXTERN(char *)
  359. PL_strtok_r(char *s1, const char *s2, char **lasts);
  360. /*
  361. * Things not (yet?) included: strspn/strcspn, strsep.
  362. * memchr, memcmp, memcpy, memccpy, index, rindex, bcmp, bcopy, bzero.
  363. * Any and all i18n/l10n stuff.
  364. */
  365. PR_END_EXTERN_C
  366. #endif /* _plstr_h */