strutil.c 27 KB

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