1
0

str.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. // TODO: Is it needed?
  164. /*
  165. char *lub_string_ndecode(const char *string, unsigned int len)
  166. {
  167. const char *s = string;
  168. char *res, *p;
  169. int esc = 0;
  170. if (!string)
  171. return NULL;
  172. p = res = malloc(len + 1);
  173. while (*s && (s < (string +len))) {
  174. if (!esc) {
  175. if ('\\' == *s)
  176. esc = 1;
  177. else
  178. *p = *s;
  179. } else {
  180. // switch (*s) {
  181. // case 'r':
  182. // case 'n':
  183. // *p = '\n';
  184. // break;
  185. // case 't':
  186. // *p = '\t';
  187. // break;
  188. // default:
  189. // *p = *s;
  190. // break;
  191. // }
  192. // *p = *s;
  193. esc = 0;
  194. }
  195. if (!esc)
  196. p++;
  197. s++;
  198. }
  199. *p = '\0';
  200. return res;
  201. }
  202. */
  203. // TODO: Is it needed?
  204. /*
  205. inline char *lub_string_decode(const char *string)
  206. {
  207. return lub_string_ndecode(string, strlen(string));
  208. }
  209. */
  210. // TODO: Is it needed?
  211. /*----------------------------------------------------------- */
  212. /*
  213. * This needs to escape any dangerous characters within the command line
  214. * to prevent gaining access to the underlying system shell.
  215. */
  216. /*
  217. char *lub_string_encode(const char *string, const char *escape_chars)
  218. {
  219. char *result = NULL;
  220. const char *p;
  221. if (!escape_chars)
  222. return lub_string_dup(string);
  223. if (string && !(*string)) // Empty string
  224. return lub_string_dup(string);
  225. for (p = string; p && *p; p++) {
  226. // find any special characters and prefix them with '\'
  227. size_t len = strcspn(p, escape_chars);
  228. lub_string_catn(&result, p, len);
  229. p += len;
  230. if (*p) {
  231. lub_string_catn(&result, "\\", 1);
  232. lub_string_catn(&result, p, 1);
  233. } else {
  234. break;
  235. }
  236. }
  237. return result;
  238. }
  239. */
  240. // TODO: Is it needed?
  241. /*--------------------------------------------------------- */
  242. /*
  243. int lub_string_nocasecmp(const char *cs, const char *ct)
  244. {
  245. int result = 0;
  246. while ((0 == result) && *cs && *ct) {
  247. //lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  248. // MACRO implementation uses braces to prevent multiple increments
  249. // when called.
  250. //
  251. int s = faux_ctype_tolower(*cs++);
  252. int t = faux_ctype_tolower(*ct++);
  253. result = s - t;
  254. }
  255. //lint -e774 Boolean within 'if' always evealuates to True
  256. // not the case because of tolower() evaluating to 0 under lint
  257. // (see above)
  258. //
  259. if (0 == result) {
  260. // account for different string lengths
  261. result = *cs - *ct;
  262. }
  263. return result;
  264. }
  265. */
  266. // TODO: Is it needed?
  267. /*--------------------------------------------------------- */
  268. /*
  269. const char *lub_string_nocasestr(const char *cs, const char *ct)
  270. {
  271. const char *p = NULL;
  272. const char *result = NULL;
  273. while (*cs) {
  274. const char *q = cs;
  275. p = ct;
  276. //lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  277. // MACRO implementation uses braces to prevent multiple increments
  278. // when called.
  279. //
  280. //lint -e506 Constant value Boolean
  281. // not the case because of tolower() evaluating to 0 under lint
  282. // (see above)
  283. //
  284. while (*p && *q
  285. && (faux_ctype_tolower(*p) == faux_ctype_tolower(*q))) {
  286. p++, q++;
  287. }
  288. if (0 == *p) {
  289. break;
  290. }
  291. cs++;
  292. }
  293. if (p && !*p) {
  294. // we've found the first match of ct within cs
  295. result = cs;
  296. }
  297. return result;
  298. }
  299. */
  300. // TODO: Is it needed?
  301. /*--------------------------------------------------------- */
  302. /*
  303. unsigned int lub_string_equal_part(const char *str1, const char *str2,
  304. bool_t utf8)
  305. {
  306. unsigned int cnt = 0;
  307. if (!str1 || !str2)
  308. return cnt;
  309. while (*str1 && *str2) {
  310. if (*str1 != *str2)
  311. break;
  312. cnt++;
  313. str1++;
  314. str2++;
  315. }
  316. if (!utf8)
  317. return cnt;
  318. // UTF8 features
  319. if (cnt && (UTF8_11 == (*(str1 - 1) & UTF8_MASK)))
  320. cnt--;
  321. return cnt;
  322. }
  323. */
  324. // TODO: Is it needed?
  325. /*--------------------------------------------------------- */
  326. /*
  327. const char *lub_string_suffix(const char *string)
  328. {
  329. const char *p1, *p2;
  330. p1 = p2 = string;
  331. while (*p1) {
  332. if (faux_ctype_isspace(*p1)) {
  333. p2 = p1;
  334. p2++;
  335. }
  336. p1++;
  337. }
  338. return p2;
  339. }
  340. */
  341. // TODO: Is it needed?
  342. /*--------------------------------------------------------- */
  343. /*
  344. const char *lub_string_nextword(const char *string,
  345. size_t *len, size_t *offset, size_t *quoted)
  346. {
  347. const char *word;
  348. *quoted = 0;
  349. // Find the start of a word (not including an opening quote)
  350. while (*string && isspace(*string)) {
  351. string++;
  352. (*offset)++;
  353. }
  354. // Is this the start of a quoted string ?
  355. if (*string == '"') {
  356. *quoted = 1;
  357. string++;
  358. }
  359. word = string;
  360. *len = 0;
  361. // Find the end of the word
  362. while (*string) {
  363. if (*string == '\\') {
  364. string++;
  365. (*len)++;
  366. if (*string) {
  367. (*len)++;
  368. string++;
  369. }
  370. continue;
  371. }
  372. // End of word
  373. if (!*quoted && isspace(*string))
  374. break;
  375. if (*string == '"') {
  376. // End of a quoted string
  377. *quoted = 2;
  378. break;
  379. }
  380. (*len)++;
  381. string++;
  382. }
  383. return word;
  384. }
  385. */
  386. // TODO: Is it needed?
  387. /*--------------------------------------------------------- */
  388. /*
  389. unsigned int lub_string_wordcount(const char *line)
  390. {
  391. const char *word;
  392. unsigned int result = 0;
  393. size_t len = 0, offset = 0;
  394. size_t quoted;
  395. for (word = lub_string_nextword(line, &len, &offset, &quoted);
  396. *word || quoted;
  397. word = lub_string_nextword(word + len, &len, &offset, &quoted)) {
  398. // account for the terminating quotation mark
  399. len += quoted ? quoted - 1 : 0;
  400. result++;
  401. }
  402. return result;
  403. }
  404. */