strutil.c 27 KB

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