strutil.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. Common strings utilities
  3. Copyright (C) 2007-2017
  4. 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 <stdlib.h>
  21. #include <langinfo.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "lib/global.h"
  25. #include "lib/util.h" /* MC_PTR_FREE */
  26. #include "lib/strutil.h"
  27. /*** global variables ****************************************************************************/
  28. GIConv str_cnv_to_term;
  29. GIConv str_cnv_from_term;
  30. GIConv str_cnv_not_convert = INVALID_CONV;
  31. /*** file scope macro definitions ****************************************************************/
  32. /*** file scope type declarations ****************************************************************/
  33. /*** file scope variables ************************************************************************/
  34. /* names, that are used for utf-8 */
  35. static const char *str_utf8_encodings[] = {
  36. "utf-8",
  37. "utf8",
  38. NULL
  39. };
  40. /* standard 8bit encodings, no wide or multibytes characters */
  41. static const char *str_8bit_encodings[] = {
  42. "cp-1251",
  43. "cp1251",
  44. "cp-1250",
  45. "cp1250",
  46. "cp-866",
  47. "cp866",
  48. "ibm-866",
  49. "ibm866",
  50. "cp-850",
  51. "cp850",
  52. "cp-852",
  53. "cp852",
  54. "iso-8859",
  55. "iso8859",
  56. "koi8",
  57. NULL
  58. };
  59. /* terminal encoding */
  60. static char *codeset = NULL;
  61. static char *term_encoding = NULL;
  62. /* function for encoding specific operations */
  63. static struct str_class used_class;
  64. /* --------------------------------------------------------------------------------------------- */
  65. /*** file scope functions ************************************************************************/
  66. /* --------------------------------------------------------------------------------------------- */
  67. /* if enc is same encoding like on terminal */
  68. static int
  69. str_test_not_convert (const char *enc)
  70. {
  71. return g_ascii_strcasecmp (enc, codeset) == 0;
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static estr_t
  75. _str_convert (GIConv coder, const char *string, int size, GString * buffer)
  76. {
  77. estr_t state = ESTR_SUCCESS;
  78. gssize left;
  79. gsize bytes_read = 0;
  80. gsize bytes_written = 0;
  81. errno = 0; /* FIXME: is it really needed? */
  82. if (coder == INVALID_CONV)
  83. return ESTR_FAILURE;
  84. if (string == NULL || buffer == NULL)
  85. return ESTR_FAILURE;
  86. /*
  87. if (! used_class.is_valid_string (string))
  88. {
  89. return ESTR_FAILURE;
  90. }
  91. */
  92. if (size < 0)
  93. size = strlen (string);
  94. else
  95. {
  96. left = strlen (string);
  97. if (left < size)
  98. size = left;
  99. }
  100. left = size;
  101. g_iconv (coder, NULL, NULL, NULL, NULL);
  102. while (left != 0)
  103. {
  104. gchar *tmp_buff;
  105. GError *mcerror = NULL;
  106. tmp_buff = g_convert_with_iconv ((const gchar *) string,
  107. left, coder, &bytes_read, &bytes_written, &mcerror);
  108. if (mcerror != NULL)
  109. {
  110. int code = mcerror->code;
  111. g_error_free (mcerror);
  112. mcerror = NULL;
  113. switch (code)
  114. {
  115. case G_CONVERT_ERROR_NO_CONVERSION:
  116. /* Conversion between the requested character sets is not supported. */
  117. tmp_buff = g_strnfill (strlen (string), '?');
  118. g_string_append (buffer, tmp_buff);
  119. g_free (tmp_buff);
  120. return ESTR_FAILURE;
  121. case G_CONVERT_ERROR_ILLEGAL_SEQUENCE:
  122. /* Invalid byte sequence in conversion input. */
  123. if ((tmp_buff == NULL) && (bytes_read != 0))
  124. /* recode valid byte sequence */
  125. tmp_buff = g_convert_with_iconv ((const gchar *) string,
  126. bytes_read, coder, NULL, NULL, NULL);
  127. if (tmp_buff != NULL)
  128. {
  129. g_string_append (buffer, tmp_buff);
  130. g_free (tmp_buff);
  131. }
  132. if ((int) bytes_read >= left)
  133. return ESTR_PROBLEM;
  134. string += bytes_read + 1;
  135. size -= (bytes_read + 1);
  136. left -= (bytes_read + 1);
  137. g_string_append_c (buffer, *(string - 1));
  138. state = ESTR_PROBLEM;
  139. break;
  140. case G_CONVERT_ERROR_PARTIAL_INPUT:
  141. /* Partial character sequence at end of input. */
  142. g_string_append (buffer, tmp_buff);
  143. g_free (tmp_buff);
  144. if ((int) bytes_read < left)
  145. {
  146. left = left - bytes_read;
  147. tmp_buff = g_strnfill (left, '?');
  148. g_string_append (buffer, tmp_buff);
  149. g_free (tmp_buff);
  150. }
  151. return ESTR_PROBLEM;
  152. case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */
  153. case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */
  154. case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */
  155. default:
  156. g_free (tmp_buff);
  157. return ESTR_FAILURE;
  158. }
  159. }
  160. else if (tmp_buff == NULL)
  161. {
  162. g_string_append (buffer, string);
  163. return ESTR_PROBLEM;
  164. }
  165. else if (*tmp_buff == '\0')
  166. {
  167. g_free (tmp_buff);
  168. g_string_append (buffer, string);
  169. return state;
  170. }
  171. else
  172. {
  173. g_string_append (buffer, tmp_buff);
  174. g_free (tmp_buff);
  175. string += bytes_read;
  176. left -= bytes_read;
  177. }
  178. }
  179. return state;
  180. }
  181. /* --------------------------------------------------------------------------------------------- */
  182. static int
  183. str_test_encoding_class (const char *encoding, const char **table)
  184. {
  185. int result = 0;
  186. if (encoding != NULL)
  187. {
  188. int t;
  189. for (t = 0; table[t] != NULL; t++)
  190. if (g_ascii_strncasecmp (encoding, table[t], strlen (table[t])) == 0)
  191. result++;
  192. }
  193. return result;
  194. }
  195. /* --------------------------------------------------------------------------------------------- */
  196. static void
  197. str_choose_str_functions (void)
  198. {
  199. if (str_test_encoding_class (codeset, str_utf8_encodings))
  200. used_class = str_utf8_init ();
  201. else if (str_test_encoding_class (codeset, str_8bit_encodings))
  202. used_class = str_8bit_init ();
  203. else
  204. used_class = str_ascii_init ();
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. /*** public functions ****************************************************************************/
  208. /* --------------------------------------------------------------------------------------------- */
  209. GIConv
  210. str_crt_conv_to (const char *to_enc)
  211. {
  212. return (!str_test_not_convert (to_enc)) ? g_iconv_open (to_enc, codeset) : str_cnv_not_convert;
  213. }
  214. /* --------------------------------------------------------------------------------------------- */
  215. GIConv
  216. str_crt_conv_from (const char *from_enc)
  217. {
  218. return (!str_test_not_convert (from_enc))
  219. ? g_iconv_open (codeset, from_enc) : str_cnv_not_convert;
  220. }
  221. /* --------------------------------------------------------------------------------------------- */
  222. void
  223. str_close_conv (GIConv conv)
  224. {
  225. if (conv != str_cnv_not_convert)
  226. g_iconv_close (conv);
  227. }
  228. /* --------------------------------------------------------------------------------------------- */
  229. estr_t
  230. str_convert (GIConv coder, const char *string, GString * buffer)
  231. {
  232. return _str_convert (coder, string, -1, buffer);
  233. }
  234. /* --------------------------------------------------------------------------------------------- */
  235. estr_t
  236. str_nconvert (GIConv coder, const char *string, int size, GString * buffer)
  237. {
  238. return _str_convert (coder, string, size, buffer);
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. gchar *
  242. str_conv_gerror_message (GError * mcerror, const char *def_msg)
  243. {
  244. return used_class.conv_gerror_message (mcerror, def_msg);
  245. }
  246. /* --------------------------------------------------------------------------------------------- */
  247. estr_t
  248. str_vfs_convert_from (GIConv coder, const char *string, GString * buffer)
  249. {
  250. estr_t result = ESTR_SUCCESS;
  251. if (coder == str_cnv_not_convert)
  252. g_string_append (buffer, string != NULL ? string : "");
  253. else
  254. result = _str_convert (coder, string, -1, buffer);
  255. return result;
  256. }
  257. /* --------------------------------------------------------------------------------------------- */
  258. estr_t
  259. str_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
  260. {
  261. return used_class.vfs_convert_to (coder, string, size, buffer);
  262. }
  263. /* --------------------------------------------------------------------------------------------- */
  264. void
  265. str_printf (GString * buffer, const char *format, ...)
  266. {
  267. va_list ap;
  268. va_start (ap, format);
  269. g_string_append_vprintf (buffer, format, ap);
  270. va_end (ap);
  271. }
  272. /* --------------------------------------------------------------------------------------------- */
  273. void
  274. str_insert_replace_char (GString * buffer)
  275. {
  276. used_class.insert_replace_char (buffer);
  277. }
  278. /* --------------------------------------------------------------------------------------------- */
  279. estr_t
  280. str_translate_char (GIConv conv, const char *keys, size_t ch_size, char *output, size_t out_size)
  281. {
  282. size_t left;
  283. size_t cnv;
  284. g_iconv (conv, NULL, NULL, NULL, NULL);
  285. left = (ch_size == (size_t) (-1)) ? strlen (keys) : ch_size;
  286. cnv = g_iconv (conv, (gchar **) & keys, &left, &output, &out_size);
  287. if (cnv == (size_t) (-1))
  288. return (errno == EINVAL) ? ESTR_PROBLEM : ESTR_FAILURE;
  289. output[0] = '\0';
  290. return ESTR_SUCCESS;
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. const char *
  294. str_detect_termencoding (void)
  295. {
  296. if (term_encoding == NULL)
  297. {
  298. /* On Linux, nl_langinfo (CODESET) returns upper case UTF-8 whether the LANG is set
  299. to utf-8 or UTF-8.
  300. On Mac OS X, it returns the same case as the LANG input.
  301. So let tranform result of nl_langinfo (CODESET) to upper case unconditionally. */
  302. term_encoding = g_ascii_strup (nl_langinfo (CODESET), -1);
  303. }
  304. return term_encoding;
  305. }
  306. /* --------------------------------------------------------------------------------------------- */
  307. gboolean
  308. str_isutf8 (const char *codeset_name)
  309. {
  310. return (str_test_encoding_class (codeset_name, str_utf8_encodings) != 0);
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. void
  314. str_init_strings (const char *termenc)
  315. {
  316. codeset = termenc != NULL ? g_ascii_strup (termenc, -1) : g_strdup (str_detect_termencoding ());
  317. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  318. if (str_cnv_not_convert == INVALID_CONV)
  319. {
  320. if (termenc != NULL)
  321. {
  322. g_free (codeset);
  323. codeset = g_strdup (str_detect_termencoding ());
  324. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  325. }
  326. if (str_cnv_not_convert == INVALID_CONV)
  327. {
  328. g_free (codeset);
  329. codeset = g_strdup (DEFAULT_CHARSET);
  330. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  331. }
  332. }
  333. str_cnv_to_term = str_cnv_not_convert;
  334. str_cnv_from_term = str_cnv_not_convert;
  335. str_choose_str_functions ();
  336. }
  337. /* --------------------------------------------------------------------------------------------- */
  338. void
  339. str_uninit_strings (void)
  340. {
  341. if (str_cnv_not_convert != INVALID_CONV)
  342. g_iconv_close (str_cnv_not_convert);
  343. /* NULL-ize pointers to avoid double free in unit tests */
  344. MC_PTR_FREE (term_encoding);
  345. MC_PTR_FREE (codeset);
  346. }
  347. /* --------------------------------------------------------------------------------------------- */
  348. const char *
  349. str_term_form (const char *text)
  350. {
  351. return used_class.term_form (text);
  352. }
  353. /* --------------------------------------------------------------------------------------------- */
  354. const char *
  355. str_fit_to_term (const char *text, int width, align_crt_t just_mode)
  356. {
  357. return used_class.fit_to_term (text, width, just_mode);
  358. }
  359. /* --------------------------------------------------------------------------------------------- */
  360. const char *
  361. str_term_trim (const char *text, int width)
  362. {
  363. return used_class.term_trim (text, width);
  364. }
  365. /* --------------------------------------------------------------------------------------------- */
  366. const char *
  367. str_term_substring (const char *text, int start, int width)
  368. {
  369. return used_class.term_substring (text, start, width);
  370. }
  371. /* --------------------------------------------------------------------------------------------- */
  372. char *
  373. str_get_next_char (char *text)
  374. {
  375. used_class.cnext_char ((const char **) &text);
  376. return text;
  377. }
  378. /* --------------------------------------------------------------------------------------------- */
  379. const char *
  380. str_cget_next_char (const char *text)
  381. {
  382. used_class.cnext_char (&text);
  383. return text;
  384. }
  385. /* --------------------------------------------------------------------------------------------- */
  386. void
  387. str_next_char (char **text)
  388. {
  389. used_class.cnext_char ((const char **) text);
  390. }
  391. /* --------------------------------------------------------------------------------------------- */
  392. void
  393. str_cnext_char (const char **text)
  394. {
  395. used_class.cnext_char (text);
  396. }
  397. /* --------------------------------------------------------------------------------------------- */
  398. char *
  399. str_get_prev_char (char *text)
  400. {
  401. used_class.cprev_char ((const char **) &text);
  402. return text;
  403. }
  404. /* --------------------------------------------------------------------------------------------- */
  405. const char *
  406. str_cget_prev_char (const char *text)
  407. {
  408. used_class.cprev_char (&text);
  409. return text;
  410. }
  411. /* --------------------------------------------------------------------------------------------- */
  412. void
  413. str_prev_char (char **text)
  414. {
  415. used_class.cprev_char ((const char **) text);
  416. }
  417. /* --------------------------------------------------------------------------------------------- */
  418. void
  419. str_cprev_char (const char **text)
  420. {
  421. used_class.cprev_char (text);
  422. }
  423. /* --------------------------------------------------------------------------------------------- */
  424. char *
  425. str_get_next_char_safe (char *text)
  426. {
  427. used_class.cnext_char_safe ((const char **) &text);
  428. return text;
  429. }
  430. /* --------------------------------------------------------------------------------------------- */
  431. const char *
  432. str_cget_next_char_safe (const char *text)
  433. {
  434. used_class.cnext_char_safe (&text);
  435. return text;
  436. }
  437. /* --------------------------------------------------------------------------------------------- */
  438. void
  439. str_next_char_safe (char **text)
  440. {
  441. used_class.cnext_char_safe ((const char **) text);
  442. }
  443. /* --------------------------------------------------------------------------------------------- */
  444. void
  445. str_cnext_char_safe (const char **text)
  446. {
  447. used_class.cnext_char_safe (text);
  448. }
  449. /* --------------------------------------------------------------------------------------------- */
  450. char *
  451. str_get_prev_char_safe (char *text)
  452. {
  453. used_class.cprev_char_safe ((const char **) &text);
  454. return text;
  455. }
  456. /* --------------------------------------------------------------------------------------------- */
  457. const char *
  458. str_cget_prev_char_safe (const char *text)
  459. {
  460. used_class.cprev_char_safe (&text);
  461. return text;
  462. }
  463. /* --------------------------------------------------------------------------------------------- */
  464. void
  465. str_prev_char_safe (char **text)
  466. {
  467. used_class.cprev_char_safe ((const char **) text);
  468. }
  469. /* --------------------------------------------------------------------------------------------- */
  470. void
  471. str_cprev_char_safe (const char **text)
  472. {
  473. used_class.cprev_char_safe (text);
  474. }
  475. /* --------------------------------------------------------------------------------------------- */
  476. int
  477. str_next_noncomb_char (char **text)
  478. {
  479. return used_class.cnext_noncomb_char ((const char **) text);
  480. }
  481. /* --------------------------------------------------------------------------------------------- */
  482. int
  483. str_cnext_noncomb_char (const char **text)
  484. {
  485. return used_class.cnext_noncomb_char (text);
  486. }
  487. /* --------------------------------------------------------------------------------------------- */
  488. int
  489. str_prev_noncomb_char (char **text, const char *begin)
  490. {
  491. return used_class.cprev_noncomb_char ((const char **) text, begin);
  492. }
  493. /* --------------------------------------------------------------------------------------------- */
  494. int
  495. str_cprev_noncomb_char (const char **text, const char *begin)
  496. {
  497. return used_class.cprev_noncomb_char (text, begin);
  498. }
  499. /* --------------------------------------------------------------------------------------------- */
  500. int
  501. str_is_valid_char (const char *ch, size_t size)
  502. {
  503. return used_class.is_valid_char (ch, size);
  504. }
  505. /* --------------------------------------------------------------------------------------------- */
  506. int
  507. str_term_width1 (const char *text)
  508. {
  509. return used_class.term_width1 (text);
  510. }
  511. /* --------------------------------------------------------------------------------------------- */
  512. int
  513. str_term_width2 (const char *text, size_t length)
  514. {
  515. return used_class.term_width2 (text, length);
  516. }
  517. /* --------------------------------------------------------------------------------------------- */
  518. int
  519. str_term_char_width (const char *text)
  520. {
  521. return used_class.term_char_width (text);
  522. }
  523. /* --------------------------------------------------------------------------------------------- */
  524. int
  525. str_offset_to_pos (const char *text, size_t length)
  526. {
  527. return used_class.offset_to_pos (text, length);
  528. }
  529. /* --------------------------------------------------------------------------------------------- */
  530. int
  531. str_length (const char *text)
  532. {
  533. return used_class.length (text);
  534. }
  535. /* --------------------------------------------------------------------------------------------- */
  536. int
  537. str_length_char (const char *text)
  538. {
  539. return str_cget_next_char_safe (text) - text;
  540. }
  541. /* --------------------------------------------------------------------------------------------- */
  542. int
  543. str_length2 (const char *text, int size)
  544. {
  545. return used_class.length2 (text, size);
  546. }
  547. /* --------------------------------------------------------------------------------------------- */
  548. int
  549. str_length_noncomb (const char *text)
  550. {
  551. return used_class.length_noncomb (text);
  552. }
  553. /* --------------------------------------------------------------------------------------------- */
  554. int
  555. str_column_to_pos (const char *text, size_t pos)
  556. {
  557. return used_class.column_to_pos (text, pos);
  558. }
  559. /* --------------------------------------------------------------------------------------------- */
  560. int
  561. str_isspace (const char *ch)
  562. {
  563. return used_class.char_isspace (ch);
  564. }
  565. /* --------------------------------------------------------------------------------------------- */
  566. int
  567. str_ispunct (const char *ch)
  568. {
  569. return used_class.char_ispunct (ch);
  570. }
  571. /* --------------------------------------------------------------------------------------------- */
  572. int
  573. str_isalnum (const char *ch)
  574. {
  575. return used_class.char_isalnum (ch);
  576. }
  577. /* --------------------------------------------------------------------------------------------- */
  578. int
  579. str_isdigit (const char *ch)
  580. {
  581. return used_class.char_isdigit (ch);
  582. }
  583. /* --------------------------------------------------------------------------------------------- */
  584. int
  585. str_toupper (const char *ch, char **out, size_t * remain)
  586. {
  587. return used_class.char_toupper (ch, out, remain);
  588. }
  589. /* --------------------------------------------------------------------------------------------- */
  590. int
  591. str_tolower (const char *ch, char **out, size_t * remain)
  592. {
  593. return used_class.char_tolower (ch, out, remain);
  594. }
  595. /* --------------------------------------------------------------------------------------------- */
  596. int
  597. str_isprint (const char *ch)
  598. {
  599. return used_class.char_isprint (ch);
  600. }
  601. /* --------------------------------------------------------------------------------------------- */
  602. gboolean
  603. str_iscombiningmark (const char *ch)
  604. {
  605. return used_class.char_iscombiningmark (ch);
  606. }
  607. /* --------------------------------------------------------------------------------------------- */
  608. const char *
  609. str_trunc (const char *text, int width)
  610. {
  611. return used_class.trunc (text, width);
  612. }
  613. /* --------------------------------------------------------------------------------------------- */
  614. char *
  615. str_create_search_needle (const char *needle, int case_sen)
  616. {
  617. return used_class.create_search_needle (needle, case_sen);
  618. }
  619. /* --------------------------------------------------------------------------------------------- */
  620. void
  621. str_release_search_needle (char *needle, int case_sen)
  622. {
  623. used_class.release_search_needle (needle, case_sen);
  624. }
  625. /* --------------------------------------------------------------------------------------------- */
  626. const char *
  627. str_search_first (const char *text, const char *search, int case_sen)
  628. {
  629. return used_class.search_first (text, search, case_sen);
  630. }
  631. /* --------------------------------------------------------------------------------------------- */
  632. const char *
  633. str_search_last (const char *text, const char *search, int case_sen)
  634. {
  635. return used_class.search_last (text, search, case_sen);
  636. }
  637. /* --------------------------------------------------------------------------------------------- */
  638. int
  639. str_is_valid_string (const char *text)
  640. {
  641. return used_class.is_valid_string (text);
  642. }
  643. /* --------------------------------------------------------------------------------------------- */
  644. int
  645. str_compare (const char *t1, const char *t2)
  646. {
  647. return used_class.compare (t1, t2);
  648. }
  649. /* --------------------------------------------------------------------------------------------- */
  650. int
  651. str_ncompare (const char *t1, const char *t2)
  652. {
  653. return used_class.ncompare (t1, t2);
  654. }
  655. /* --------------------------------------------------------------------------------------------- */
  656. int
  657. str_casecmp (const char *t1, const char *t2)
  658. {
  659. return used_class.casecmp (t1, t2);
  660. }
  661. /* --------------------------------------------------------------------------------------------- */
  662. int
  663. str_ncasecmp (const char *t1, const char *t2)
  664. {
  665. return used_class.ncasecmp (t1, t2);
  666. }
  667. /* --------------------------------------------------------------------------------------------- */
  668. int
  669. str_prefix (const char *text, const char *prefix)
  670. {
  671. return used_class.prefix (text, prefix);
  672. }
  673. /* --------------------------------------------------------------------------------------------- */
  674. int
  675. str_caseprefix (const char *text, const char *prefix)
  676. {
  677. return used_class.caseprefix (text, prefix);
  678. }
  679. /* --------------------------------------------------------------------------------------------- */
  680. void
  681. str_fix_string (char *text)
  682. {
  683. used_class.fix_string (text);
  684. }
  685. /* --------------------------------------------------------------------------------------------- */
  686. char *
  687. str_create_key (const char *text, int case_sen)
  688. {
  689. return used_class.create_key (text, case_sen);
  690. }
  691. /* --------------------------------------------------------------------------------------------- */
  692. char *
  693. str_create_key_for_filename (const char *text, int case_sen)
  694. {
  695. return used_class.create_key_for_filename (text, case_sen);
  696. }
  697. /* --------------------------------------------------------------------------------------------- */
  698. int
  699. str_key_collate (const char *t1, const char *t2, int case_sen)
  700. {
  701. return used_class.key_collate (t1, t2, case_sen);
  702. }
  703. /* --------------------------------------------------------------------------------------------- */
  704. void
  705. str_release_key (char *key, int case_sen)
  706. {
  707. used_class.release_key (key, case_sen);
  708. }
  709. /* --------------------------------------------------------------------------------------------- */
  710. void
  711. str_msg_term_size (const char *text, int *lines, int *columns)
  712. {
  713. char *p, *tmp;
  714. char *q;
  715. char c = '\0';
  716. *lines = 1;
  717. *columns = 0;
  718. tmp = g_strdup (text);
  719. p = tmp;
  720. while (TRUE)
  721. {
  722. int width;
  723. q = strchr (p, '\n');
  724. if (q != NULL)
  725. {
  726. c = q[0];
  727. q[0] = '\0';
  728. }
  729. width = str_term_width1 (p);
  730. if (width > *columns)
  731. *columns = width;
  732. if (q == NULL)
  733. break;
  734. q[0] = c;
  735. p = q + 1;
  736. (*lines)++;
  737. }
  738. g_free (tmp);
  739. }
  740. /* --------------------------------------------------------------------------------------------- */
  741. char *
  742. strrstr_skip_count (const char *haystack, const char *needle, size_t skip_count)
  743. {
  744. char *semi;
  745. ssize_t len;
  746. len = strlen (haystack);
  747. do
  748. {
  749. semi = g_strrstr_len (haystack, len, needle);
  750. if (semi == NULL)
  751. return NULL;
  752. len = semi - haystack - 1;
  753. }
  754. while (skip_count-- != 0);
  755. return semi;
  756. }
  757. /* --------------------------------------------------------------------------------------------- */
  758. /* Interprete string as a non-negative decimal integer, optionally multiplied by various values.
  759. *
  760. * @param str input value
  761. * @param invalid set to TRUE if "str" does not represent a number in this format
  762. *
  763. * @return non-integer representation of "str", 0 in case of error.
  764. */
  765. uintmax_t
  766. parse_integer (const char *str, gboolean * invalid)
  767. {
  768. uintmax_t n;
  769. char *suffix;
  770. strtol_error_t e;
  771. e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
  772. if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
  773. {
  774. uintmax_t multiplier;
  775. multiplier = parse_integer (suffix + 1, invalid);
  776. if (multiplier != 0 && n * multiplier / multiplier != n)
  777. {
  778. *invalid = TRUE;
  779. return 0;
  780. }
  781. n *= multiplier;
  782. }
  783. else if (e != LONGINT_OK)
  784. {
  785. *invalid = TRUE;
  786. n = 0;
  787. }
  788. return n;
  789. }
  790. /* --------------------------------------------------------------------------------------------- */