1
0

str.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /** @file str.c
  2. * @brief String related functions
  3. *
  4. * This file implements some often used string functions.
  5. * Some functions are more portable versions of standard
  6. * functions but others are original ones.
  7. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "faux/ctype.h"
  11. #include "faux/str.h"
  12. /* TODO: Are that vars really needed? */
  13. //const char *lub_string_esc_default = "`|$<>&()#;\\\"!";
  14. //const char *lub_string_esc_regex = "^$.*+[](){}";
  15. //const char *lub_string_esc_quoted = "\\\"";
  16. /** @brief Free the memory allocated for the string.
  17. *
  18. * Safely free the memory allocated for the string. You can use NULL
  19. * pointer with this function. POSIX's free() checks for the NULL pointer
  20. * but not all systems do so.
  21. *
  22. * @param [in] str String to free
  23. */
  24. void faux_str_free(char *str) {
  25. faux_free(str);
  26. }
  27. /** @brief Duplicates the string.
  28. *
  29. * Duplicates the string. Same as standard strdup() function. Allocates
  30. * memory with malloc(). Checks for NULL pointer.
  31. *
  32. * @warning Resulting string must be freed by faux_str_free().
  33. *
  34. * @param [in] str String to duplicate.
  35. * @return Pointer to allocated string or NULL.
  36. */
  37. char *faux_str_dup(const char *str) {
  38. if (!str)
  39. return NULL;
  40. return strdup(str);
  41. }
  42. /** @brief Duplicates the first n bytes of the string.
  43. *
  44. * Duplicates at most n bytes of the string. Allocates
  45. * memory with malloc(). Checks for NULL pointer. Function will allocate
  46. * n + 1 bytes to store string and terminating null byte.
  47. *
  48. * @warning Resulting string must be freed by faux_str_free().
  49. *
  50. * @param [in] str String to duplicate.
  51. * @param [in] n Number of bytes to copy.
  52. * @return Pointer to allocated string or NULL.
  53. */
  54. char *faux_str_dupn(const char *str, size_t n) {
  55. char *res = NULL;
  56. size_t len = 0;
  57. if (!str)
  58. return NULL;
  59. len = strlen(str);
  60. len = (len < n) ? len : n;
  61. res = faux_zmalloc(len + 1);
  62. if (!res)
  63. return NULL;
  64. strncpy(res, str, len);
  65. res[len] = '\0';
  66. return res;
  67. }
  68. /** @brief Generates lowercase copy of input string.
  69. *
  70. * Allocates the copy of input string and convert that copy to lowercase.
  71. *
  72. * @warning Resulting string must be freed by faux_str_free().
  73. *
  74. * @param [in] str String to convert.
  75. * @return Pointer to lowercase string copy or NULL.
  76. */
  77. char *faux_str_tolower(const char *str)
  78. {
  79. char *res = faux_str_dup(str);
  80. char *p = res;
  81. if (!res)
  82. return NULL;
  83. while (*p) {
  84. *p = faux_ctype_tolower(*p);
  85. p++;
  86. }
  87. return res;
  88. }
  89. /** @brief Generates uppercase copy of input string.
  90. *
  91. * Allocates the copy of input string and convert that copy to uppercase.
  92. *
  93. * @warning Resulting string must be freed by faux_str_free().
  94. *
  95. * @param [in] str String to convert.
  96. * @return Pointer to lowercase string copy or NULL.
  97. */
  98. char *faux_str_toupper(const char *str)
  99. {
  100. char *res = faux_str_dup(str);
  101. char *p = res;
  102. if (!res)
  103. return NULL;
  104. while (*p) {
  105. *p = faux_ctype_toupper(*p);
  106. p++;
  107. }
  108. return res;
  109. }
  110. /** @brief Add n bytes of text to existent string.
  111. *
  112. * Concatenate two strings. Add n bytes of second string to the end of the
  113. * first one. The first argument is address of string pointer. The pointer
  114. * can be changed due to realloc() features. The first pointer can be NULL.
  115. * In this case the memory will be malloc()-ed and stored to the first pointer.
  116. *
  117. * @param [in,out] str Address of first string pointer.
  118. * @param [in] text Text to add to the first string.
  119. * @param [in] n Number of bytes to add.
  120. * @return Pointer to resulting string or NULL.
  121. */
  122. char *faux_str_catn(char **str, const char *text, size_t n) {
  123. size_t str_len = 0;
  124. size_t text_len = 0;
  125. char *res = NULL;
  126. char *p = NULL;
  127. if (!text)
  128. return *str;
  129. str_len = (*str) ? strlen(*str) : 0;
  130. text_len = strlen(text);
  131. text_len = (text_len < n) ? text_len : n;
  132. res = realloc(*str, str_len + text_len + 1);
  133. if (!res)
  134. return NULL;
  135. p = res + str_len;
  136. strncpy(p, text, text_len);
  137. p[text_len] = '\0';
  138. *str = res;
  139. return res;
  140. }
  141. /** @brief Add some text to existent string.
  142. *
  143. * Concatenate two strings. Add second string to the end of the first one.
  144. * The first argument is address of string pointer. The pointer can be
  145. * changed due to realloc() features. The first pointer can be NULL. In this
  146. * case the memory will be malloc()-ed and stored to the first pointer.
  147. *
  148. * @param [in,out] str Address of first string pointer.
  149. * @param [in] text Text to add to the first string.
  150. * @return Pointer to resulting string or NULL.
  151. */
  152. char *faux_str_cat(char **str, const char *text) {
  153. size_t len = 0;
  154. if (!text)
  155. return *str;
  156. len = strlen(text);
  157. return faux_str_catn(str, text, len);
  158. }
  159. /** @brief Compare n first characters of two strings ignoring case.
  160. *
  161. * The difference beetween this function an standard strncasecmp() is
  162. * faux function uses faux ctype functions. It can be important for
  163. * portability.
  164. *
  165. * @param [in] str1 First string to compare.
  166. * @param [in] str2 Second string to compare.
  167. * @param [in] n Number of characters to compare.
  168. * @return < 0, 0, > 0, see the strcasecmp().
  169. */
  170. int faux_str_ncasecmp(const char *str1, const char *str2, size_t n) {
  171. const char *p1 = str1;
  172. const char *p2 = str2;
  173. size_t num = n;
  174. while ((*p1 || *p2) && num) {
  175. int res = 0;
  176. char c1 = faux_ctype_tolower(*p1);
  177. char c2 = faux_ctype_tolower(*p2);
  178. res = c1 - c2;
  179. if (res)
  180. return res;
  181. p1++;
  182. p2++;
  183. num--;
  184. }
  185. return 0;
  186. }
  187. /** @brief Compare two strings ignoring case.
  188. *
  189. * The difference beetween this function an standard strcasecmp() is
  190. * faux function uses faux ctype functions. It can be important for
  191. * portability.
  192. *
  193. * @param [in] str1 First string to compare.
  194. * @param [in] str2 Second string to compare.
  195. * @return < 0, 0, > 0, see the strcasecmp().
  196. */
  197. int faux_str_casecmp(const char *str1, const char *str2) {
  198. const char *p1 = str1;
  199. const char *p2 = str2;
  200. while (*p1 || *p2) {
  201. int res = 0;
  202. char c1 = faux_ctype_tolower(*p1);
  203. char c2 = faux_ctype_tolower(*p2);
  204. res = c1 - c2;
  205. if (res)
  206. return res;
  207. p1++;
  208. p2++;
  209. }
  210. return 0;
  211. }
  212. const char *lub_string_nocasestr(const char *cs, const char *ct)
  213. {
  214. const char *p = NULL;
  215. const char *result = NULL;
  216. while (*cs) {
  217. const char *q = cs;
  218. p = ct;
  219. while (*p && *q
  220. && (faux_ctype_tolower(*p) == faux_ctype_tolower(*q))) {
  221. p++, q++;
  222. }
  223. if (0 == *p) {
  224. break;
  225. }
  226. cs++;
  227. }
  228. if (p && !*p) {
  229. result = cs;
  230. }
  231. return result;
  232. }
  233. // TODO: Is it needed?
  234. /*
  235. char *lub_string_ndecode(const char *string, unsigned int len)
  236. {
  237. const char *s = string;
  238. char *res, *p;
  239. int esc = 0;
  240. if (!string)
  241. return NULL;
  242. p = res = faux_zmalloc(len + 1);
  243. while (*s && (s < (string +len))) {
  244. if (!esc) {
  245. if ('\\' == *s)
  246. esc = 1;
  247. else
  248. *p = *s;
  249. } else {
  250. // switch (*s) {
  251. // case 'r':
  252. // case 'n':
  253. // *p = '\n';
  254. // break;
  255. // case 't':
  256. // *p = '\t';
  257. // break;
  258. // default:
  259. // *p = *s;
  260. // break;
  261. // }
  262. // *p = *s;
  263. esc = 0;
  264. }
  265. if (!esc)
  266. p++;
  267. s++;
  268. }
  269. *p = '\0';
  270. return res;
  271. }
  272. */
  273. // TODO: Is it needed?
  274. /*
  275. inline char *lub_string_decode(const char *string)
  276. {
  277. return lub_string_ndecode(string, strlen(string));
  278. }
  279. */
  280. // TODO: Is it needed?
  281. /*----------------------------------------------------------- */
  282. /*
  283. * This needs to escape any dangerous characters within the command line
  284. * to prevent gaining access to the underlying system shell.
  285. */
  286. /*
  287. char *lub_string_encode(const char *string, const char *escape_chars)
  288. {
  289. char *result = NULL;
  290. const char *p;
  291. if (!escape_chars)
  292. return lub_string_dup(string);
  293. if (string && !(*string)) // Empty string
  294. return lub_string_dup(string);
  295. for (p = string; p && *p; p++) {
  296. // find any special characters and prefix them with '\'
  297. size_t len = strcspn(p, escape_chars);
  298. lub_string_catn(&result, p, len);
  299. p += len;
  300. if (*p) {
  301. lub_string_catn(&result, "\\", 1);
  302. lub_string_catn(&result, p, 1);
  303. } else {
  304. break;
  305. }
  306. }
  307. return result;
  308. }
  309. */
  310. // TODO: Is it needed?
  311. /*--------------------------------------------------------- */
  312. /*
  313. unsigned int lub_string_equal_part(const char *str1, const char *str2,
  314. bool_t utf8)
  315. {
  316. unsigned int cnt = 0;
  317. if (!str1 || !str2)
  318. return cnt;
  319. while (*str1 && *str2) {
  320. if (*str1 != *str2)
  321. break;
  322. cnt++;
  323. str1++;
  324. str2++;
  325. }
  326. if (!utf8)
  327. return cnt;
  328. // UTF8 features
  329. if (cnt && (UTF8_11 == (*(str1 - 1) & UTF8_MASK)))
  330. cnt--;
  331. return cnt;
  332. }
  333. */
  334. // TODO: Is it needed?
  335. /*--------------------------------------------------------- */
  336. /*
  337. const char *lub_string_suffix(const char *string)
  338. {
  339. const char *p1, *p2;
  340. p1 = p2 = string;
  341. while (*p1) {
  342. if (faux_ctype_isspace(*p1)) {
  343. p2 = p1;
  344. p2++;
  345. }
  346. p1++;
  347. }
  348. return p2;
  349. }
  350. */
  351. // TODO: Is it needed?
  352. /*--------------------------------------------------------- */
  353. /*
  354. const char *lub_string_nextword(const char *string,
  355. size_t *len, size_t *offset, size_t *quoted)
  356. {
  357. const char *word;
  358. *quoted = 0;
  359. // Find the start of a word (not including an opening quote)
  360. while (*string && isspace(*string)) {
  361. string++;
  362. (*offset)++;
  363. }
  364. // Is this the start of a quoted string ?
  365. if (*string == '"') {
  366. *quoted = 1;
  367. string++;
  368. }
  369. word = string;
  370. *len = 0;
  371. // Find the end of the word
  372. while (*string) {
  373. if (*string == '\\') {
  374. string++;
  375. (*len)++;
  376. if (*string) {
  377. (*len)++;
  378. string++;
  379. }
  380. continue;
  381. }
  382. // End of word
  383. if (!*quoted && isspace(*string))
  384. break;
  385. if (*string == '"') {
  386. // End of a quoted string
  387. *quoted = 2;
  388. break;
  389. }
  390. (*len)++;
  391. string++;
  392. }
  393. return word;
  394. }
  395. */
  396. // TODO: Is it needed?
  397. /*--------------------------------------------------------- */
  398. /*
  399. unsigned int lub_string_wordcount(const char *line)
  400. {
  401. const char *word;
  402. unsigned int result = 0;
  403. size_t len = 0, offset = 0;
  404. size_t quoted;
  405. for (word = lub_string_nextword(line, &len, &offset, &quoted);
  406. *word || quoted;
  407. word = lub_string_nextword(word + len, &len, &offset, &quoted)) {
  408. // account for the terminating quotation mark
  409. len += quoted ? quoted - 1 : 0;
  410. result++;
  411. }
  412. return result;
  413. }
  414. */