strutilascii.c 17 KB

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