strutil.c 27 KB

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