strutilascii.c 16 KB

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