str.c 9.9 KB

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