strutil.c 27 KB

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