strutilascii.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /* ASCII strings utilities
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. Written 2007 by:
  4. Rostislav Benes
  5. The file_date routine is mostly from GNU's fileutils package,
  6. written by Richard Stallman and David MacKenzie.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <config.h>
  23. #include <errno.h>
  24. #include "lib/global.h"
  25. #include "lib/strutil.h"
  26. /* using g_ascii function from glib
  27. * on terminal are showed only ascii characters (lower then 0x80)
  28. */
  29. static const char replch = '?';
  30. static void
  31. str_ascii_insert_replace_char (GString * buffer)
  32. {
  33. g_string_append_c (buffer, replch);
  34. }
  35. static int
  36. str_ascii_is_valid_string (const char *text)
  37. {
  38. (void) text;
  39. return 1;
  40. }
  41. static int
  42. str_ascii_is_valid_char (const char *ch, size_t size)
  43. {
  44. (void) ch;
  45. (void) size;
  46. return 1;
  47. }
  48. static void
  49. str_ascii_cnext_char (const char **text)
  50. {
  51. (*text)++;
  52. }
  53. static void
  54. str_ascii_cprev_char (const char **text)
  55. {
  56. (*text)--;
  57. }
  58. static int
  59. str_ascii_cnext_noncomb_char (const char **text)
  60. {
  61. if (*text[0] != '\0')
  62. {
  63. (*text)++;
  64. return 1;
  65. }
  66. else
  67. return 0;
  68. }
  69. static int
  70. str_ascii_cprev_noncomb_char (const char **text, const char *begin)
  71. {
  72. if ((*text) != begin)
  73. {
  74. (*text)--;
  75. return 1;
  76. }
  77. else
  78. return 0;
  79. }
  80. static int
  81. str_ascii_isspace (const char *text)
  82. {
  83. return g_ascii_isspace ((gchar) text[0]);
  84. }
  85. static int
  86. str_ascii_ispunct (const char *text)
  87. {
  88. return g_ascii_ispunct ((gchar) text[0]);
  89. }
  90. static int
  91. str_ascii_isalnum (const char *text)
  92. {
  93. return g_ascii_isalnum ((gchar) text[0]);
  94. }
  95. static int
  96. str_ascii_isdigit (const char *text)
  97. {
  98. return g_ascii_isdigit ((gchar) text[0]);
  99. }
  100. static int
  101. str_ascii_isprint (const char *text)
  102. {
  103. return g_ascii_isprint ((gchar) text[0]);
  104. }
  105. static int
  106. str_ascii_iscombiningmark (const char *text)
  107. {
  108. (void) text;
  109. return 0;
  110. }
  111. static int
  112. str_ascii_toupper (const char *text, char **out, size_t * remain)
  113. {
  114. if (*remain <= 1)
  115. return 0;
  116. (*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
  117. (*out)++;
  118. (*remain)--;
  119. return 1;
  120. }
  121. static int
  122. str_ascii_tolower (const char *text, char **out, size_t * remain)
  123. {
  124. if (*remain <= 1)
  125. return 0;
  126. (*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
  127. (*out)++;
  128. (*remain)--;
  129. return 1;
  130. }
  131. static int
  132. str_ascii_length (const char *text)
  133. {
  134. return strlen (text);
  135. }
  136. static int
  137. str_ascii_length2 (const char *text, int size)
  138. {
  139. return (size >= 0) ? min (strlen (text), (gsize) size) : strlen (text);
  140. }
  141. static gchar *
  142. str_ascii_conv_gerror_message (GError * error, const char *def_msg)
  143. {
  144. /* the same as str_utf8_conv_gerror_message() */
  145. if ((error != NULL) && (error->message != NULL))
  146. return g_strdup (error->message);
  147. return g_strdup (def_msg != NULL ? def_msg : "");
  148. }
  149. static estr_t
  150. str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
  151. {
  152. (void) coder;
  153. g_string_append_len (buffer, string, size);
  154. return ESTR_SUCCESS;
  155. }
  156. static const char *
  157. str_ascii_term_form (const char *text)
  158. {
  159. static char result[BUF_MEDIUM];
  160. char *actual;
  161. size_t remain;
  162. size_t length;
  163. size_t pos = 0;
  164. actual = result;
  165. remain = sizeof (result);
  166. length = strlen (text);
  167. /* go throw all characters and check, if they are ascii and printable */
  168. for (; pos < length && remain > 1; pos++, actual++, remain--)
  169. {
  170. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  171. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  172. }
  173. actual[0] = '\0';
  174. return result;
  175. }
  176. static const char *
  177. str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
  178. {
  179. static char result[BUF_MEDIUM];
  180. char *actual;
  181. size_t remain;
  182. int ident;
  183. size_t length;
  184. size_t pos = 0;
  185. length = strlen (text);
  186. actual = result;
  187. remain = sizeof (result);
  188. if ((int) length <= width)
  189. {
  190. ident = 0;
  191. switch (HIDE_FIT (just_mode))
  192. {
  193. case J_CENTER_LEFT:
  194. case J_CENTER:
  195. ident = (width - length) / 2;
  196. break;
  197. case J_RIGHT:
  198. ident = width - length;
  199. break;
  200. }
  201. /* add space before text */
  202. if ((int) remain <= ident)
  203. goto finally;
  204. memset (actual, ' ', ident);
  205. actual += ident;
  206. remain -= ident;
  207. /* copy all characters */
  208. for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
  209. {
  210. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  211. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  212. }
  213. /* add space after text */
  214. if (width - length - ident > 0)
  215. {
  216. if (remain <= width - length - ident)
  217. goto finally;
  218. memset (actual, ' ', width - length - ident);
  219. actual += width - length - ident;
  220. remain -= width - length - ident;
  221. }
  222. }
  223. else
  224. {
  225. if (IS_FIT (just_mode))
  226. {
  227. /* copy prefix of text, that is not wider than width / 2 */
  228. for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
  229. {
  230. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  231. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  232. }
  233. if (remain <= 1)
  234. goto finally;
  235. actual[0] = '~';
  236. actual++;
  237. remain--;
  238. pos += length - width + 1;
  239. /* copy suffix of text */
  240. for (; pos < length && remain > 1; pos++, actual++, remain--)
  241. {
  242. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  243. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  244. }
  245. }
  246. else
  247. {
  248. ident = 0;
  249. switch (HIDE_FIT (just_mode))
  250. {
  251. case J_CENTER:
  252. ident = (length - width) / 2;
  253. break;
  254. case J_RIGHT:
  255. ident = length - width;
  256. break;
  257. }
  258. /* copy substring text, substring start from ident and take width
  259. * characters from text */
  260. pos += ident;
  261. for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
  262. {
  263. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  264. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  265. }
  266. }
  267. }
  268. finally:
  269. actual[0] = '\0';
  270. return result;
  271. }
  272. static const char *
  273. str_ascii_term_trim (const char *text, int width)
  274. {
  275. static char result[BUF_MEDIUM];
  276. size_t remain;
  277. char *actual;
  278. size_t pos = 0;
  279. size_t length;
  280. length = strlen (text);
  281. actual = result;
  282. remain = sizeof (result);
  283. if (width < (int) length)
  284. {
  285. if (width <= 3)
  286. {
  287. memset (actual, '.', width);
  288. actual += width;
  289. remain -= width;
  290. }
  291. else
  292. {
  293. memset (actual, '.', 3);
  294. actual += 3;
  295. remain -= 3;
  296. pos += length - width + 3;
  297. /* copy suffix of text */
  298. for (; pos < length && remain > 1; pos++, actual++, remain--)
  299. {
  300. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  301. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  302. }
  303. }
  304. }
  305. else
  306. {
  307. /* copy all characters */
  308. for (; pos < length && remain > 1; pos++, actual++, remain--)
  309. {
  310. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  311. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  312. }
  313. }
  314. actual[0] = '\0';
  315. return result;
  316. }
  317. static int
  318. str_ascii_term_width2 (const char *text, size_t length)
  319. {
  320. return (length != (size_t) (-1)) ? min (strlen (text), length) : strlen (text);
  321. }
  322. static int
  323. str_ascii_term_width1 (const char *text)
  324. {
  325. return str_ascii_term_width2 (text, (size_t) (-1));
  326. }
  327. static int
  328. str_ascii_term_char_width (const char *text)
  329. {
  330. (void) text;
  331. return 1;
  332. }
  333. static const char *
  334. str_ascii_term_substring (const char *text, int start, int width)
  335. {
  336. static char result[BUF_MEDIUM];
  337. size_t remain;
  338. char *actual;
  339. size_t pos = 0;
  340. size_t length;
  341. actual = result;
  342. remain = sizeof (result);
  343. length = strlen (text);
  344. if (start < (int) length)
  345. {
  346. pos += start;
  347. /* copy at most width characters from text from start */
  348. for (; pos < length && width > 0 && remain > 1; pos++, width--, actual++, remain--)
  349. {
  350. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  351. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  352. }
  353. }
  354. /* if text is shorter then width, add space to the end */
  355. for (; width > 0 && remain > 1; actual++, remain--, width--)
  356. {
  357. actual[0] = ' ';
  358. }
  359. actual[0] = '\0';
  360. return result;
  361. }
  362. static const char *
  363. str_ascii_trunc (const char *text, int width)
  364. {
  365. static char result[MC_MAXPATHLEN];
  366. int remain;
  367. char *actual;
  368. size_t pos = 0;
  369. size_t length;
  370. actual = result;
  371. remain = sizeof (result);
  372. length = strlen (text);
  373. if ((int) length > width)
  374. {
  375. /* copy prefix of text */
  376. for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
  377. {
  378. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  379. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  380. }
  381. if (remain <= 1)
  382. goto finally;
  383. actual[0] = '~';
  384. actual++;
  385. remain--;
  386. pos += length - width + 1;
  387. /* copy suffix of text */
  388. for (; pos < length && remain > 1; pos++, actual++, remain--)
  389. {
  390. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  391. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  392. }
  393. }
  394. else
  395. {
  396. /* copy all characters */
  397. for (; pos < length && remain > 1; pos++, actual++, remain--)
  398. {
  399. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  400. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  401. }
  402. }
  403. finally:
  404. actual[0] = '\0';
  405. return result;
  406. }
  407. static int
  408. str_ascii_offset_to_pos (const char *text, size_t length)
  409. {
  410. (void) text;
  411. return (int) length;
  412. }
  413. static int
  414. str_ascii_column_to_pos (const char *text, size_t pos)
  415. {
  416. (void) text;
  417. return (int) pos;
  418. }
  419. static char *
  420. str_ascii_create_search_needle (const char *needle, int case_sen)
  421. {
  422. (void) case_sen;
  423. return (char *) needle;
  424. }
  425. static void
  426. str_ascii_release_search_needle (char *needle, int case_sen)
  427. {
  428. (void) case_sen;
  429. (void) needle;
  430. }
  431. static const char *
  432. str_ascii_search_first (const char *text, const char *search, int case_sen)
  433. {
  434. char *fold_text;
  435. char *fold_search;
  436. const char *match;
  437. size_t offset;
  438. fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
  439. fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
  440. match = g_strstr_len (fold_text, -1, fold_search);
  441. if (match != NULL)
  442. {
  443. offset = match - fold_text;
  444. match = text + offset;
  445. }
  446. if (!case_sen)
  447. {
  448. g_free (fold_text);
  449. g_free (fold_search);
  450. }
  451. return match;
  452. }
  453. static const char *
  454. str_ascii_search_last (const char *text, const char *search, int case_sen)
  455. {
  456. char *fold_text;
  457. char *fold_search;
  458. const char *match;
  459. size_t offset;
  460. fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
  461. fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
  462. match = g_strrstr_len (fold_text, -1, fold_search);
  463. if (match != NULL)
  464. {
  465. offset = match - fold_text;
  466. match = text + offset;
  467. }
  468. if (!case_sen)
  469. {
  470. g_free (fold_text);
  471. g_free (fold_search);
  472. }
  473. return match;
  474. }
  475. static int
  476. str_ascii_compare (const char *t1, const char *t2)
  477. {
  478. return strcmp (t1, t2);
  479. }
  480. static int
  481. str_ascii_ncompare (const char *t1, const char *t2)
  482. {
  483. return strncmp (t1, t2, min (strlen (t1), strlen (t2)));
  484. }
  485. static int
  486. str_ascii_casecmp (const char *t1, const char *t2)
  487. {
  488. return g_ascii_strcasecmp (t1, t2);
  489. }
  490. static int
  491. str_ascii_ncasecmp (const char *t1, const char *t2)
  492. {
  493. return g_ascii_strncasecmp (t1, t2, min (strlen (t1), strlen (t2)));
  494. }
  495. static void
  496. str_ascii_fix_string (char *text)
  497. {
  498. for (; text[0] != '\0'; text++)
  499. {
  500. text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
  501. }
  502. }
  503. static char *
  504. str_ascii_create_key (const char *text, int case_sen)
  505. {
  506. (void) case_sen;
  507. return (char *) text;
  508. }
  509. static int
  510. str_ascii_key_collate (const char *t1, const char *t2, int case_sen)
  511. {
  512. return (case_sen) ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
  513. }
  514. static void
  515. str_ascii_release_key (char *key, int case_sen)
  516. {
  517. (void) key;
  518. (void) case_sen;
  519. }
  520. static int
  521. str_ascii_prefix (const char *text, const char *prefix)
  522. {
  523. int result;
  524. for (result = 0; text[result] != '\0' && prefix[result] != '\0'
  525. && text[result] == prefix[result]; result++);
  526. return result;
  527. }
  528. static int
  529. str_ascii_caseprefix (const char *text, const char *prefix)
  530. {
  531. int result;
  532. for (result = 0; text[result] != '\0' && prefix[result] != '\0'
  533. && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);
  534. return result;
  535. }
  536. struct str_class
  537. str_ascii_init (void)
  538. {
  539. struct str_class result;
  540. result.conv_gerror_message = str_ascii_conv_gerror_message;
  541. result.vfs_convert_to = str_ascii_vfs_convert_to;
  542. result.insert_replace_char = str_ascii_insert_replace_char;
  543. result.is_valid_string = str_ascii_is_valid_string;
  544. result.is_valid_char = str_ascii_is_valid_char;
  545. result.cnext_char = str_ascii_cnext_char;
  546. result.cprev_char = str_ascii_cprev_char;
  547. result.cnext_char_safe = str_ascii_cnext_char;
  548. result.cprev_char_safe = str_ascii_cprev_char;
  549. result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
  550. result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
  551. result.isspace = str_ascii_isspace;
  552. result.ispunct = str_ascii_ispunct;
  553. result.isalnum = str_ascii_isalnum;
  554. result.isdigit = str_ascii_isdigit;
  555. result.isprint = str_ascii_isprint;
  556. result.iscombiningmark = str_ascii_iscombiningmark;
  557. result.toupper = str_ascii_toupper;
  558. result.tolower = str_ascii_tolower;
  559. result.length = str_ascii_length;
  560. result.length2 = str_ascii_length2;
  561. result.length_noncomb = str_ascii_length;
  562. result.fix_string = str_ascii_fix_string;
  563. result.term_form = str_ascii_term_form;
  564. result.fit_to_term = str_ascii_fit_to_term;
  565. result.term_trim = str_ascii_term_trim;
  566. result.term_width2 = str_ascii_term_width2;
  567. result.term_width1 = str_ascii_term_width1;
  568. result.term_char_width = str_ascii_term_char_width;
  569. result.term_substring = str_ascii_term_substring;
  570. result.trunc = str_ascii_trunc;
  571. result.offset_to_pos = str_ascii_offset_to_pos;
  572. result.column_to_pos = str_ascii_column_to_pos;
  573. result.create_search_needle = str_ascii_create_search_needle;
  574. result.release_search_needle = str_ascii_release_search_needle;
  575. result.search_first = str_ascii_search_first;
  576. result.search_last = str_ascii_search_last;
  577. result.compare = str_ascii_compare;
  578. result.ncompare = str_ascii_ncompare;
  579. result.casecmp = str_ascii_casecmp;
  580. result.ncasecmp = str_ascii_ncasecmp;
  581. result.prefix = str_ascii_prefix;
  582. result.caseprefix = str_ascii_caseprefix;
  583. result.create_key = str_ascii_create_key;
  584. result.create_key_for_filename = str_ascii_create_key;
  585. result.key_collate = str_ascii_key_collate;
  586. result.release_key = str_ascii_release_key;
  587. return result;
  588. }