fixed relative symlink operations. Symlink now stay relative
[midnight-commander.git] / lib / strutil / strutilutf8.c
blob59be6b2cec0dd19ca74a4cb86e44d19f36604d6b
1 /* UTF-8 strings utilities
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 Written 2007 by:
5 Rostislav Benes
7 The file_date routine is mostly from GNU's fileutils package,
8 written by Richard Stallman and David MacKenzie.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <config.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <glib.h>
30 #include <langinfo.h>
31 #include <string.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
36 /* using function for utf-8 from glib */
38 static const char replch[] = "\xEF\xBF\xBD";
40 static int
41 str_unichar_iscombiningmark (gunichar uni)
43 int type = g_unichar_type (uni);
44 return (type == G_UNICODE_COMBINING_MARK)
45 || (type == G_UNICODE_ENCLOSING_MARK) || (type == G_UNICODE_NON_SPACING_MARK);
48 static void
49 str_utf8_insert_replace_char (GString * buffer)
51 g_string_append (buffer, replch);
54 static int
55 str_utf8_is_valid_string (const char *text)
57 return g_utf8_validate (text, -1, NULL);
60 static int
61 str_utf8_is_valid_char (const char *ch, size_t size)
63 switch (g_utf8_get_char_validated (ch, size))
65 case (gunichar) (-2):
66 return -2;
67 case (gunichar) (-1):
68 return -1;
69 default:
70 return 1;
74 static void
75 str_utf8_cnext_char (const char **text)
77 (*text) = g_utf8_next_char (*text);
80 static void
81 str_utf8_cprev_char (const char **text)
83 (*text) = g_utf8_prev_char (*text);
86 static void
87 str_utf8_cnext_char_safe (const char **text)
89 if (str_utf8_is_valid_char (*text, -1) == 1)
90 (*text) = g_utf8_next_char (*text);
91 else
92 (*text)++;
95 static void
96 str_utf8_cprev_char_safe (const char **text)
98 const char *result = g_utf8_prev_char (*text);
99 const char *t = result;
100 str_utf8_cnext_char_safe (&t);
101 if (t == *text)
102 (*text) = result;
103 else
104 (*text)--;
107 static void
108 str_utf8_fix_string (char *text)
110 gunichar uni;
112 while (text[0] != '\0')
114 uni = g_utf8_get_char_validated (text, -1);
115 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
117 text = g_utf8_next_char (text);
119 else
121 text[0] = '?';
122 text++;
127 static int
128 str_utf8_isspace (const char *text)
130 gunichar uni = g_utf8_get_char_validated (text, -1);
131 return g_unichar_isspace (uni);
134 static int
135 str_utf8_ispunct (const char *text)
137 gunichar uni = g_utf8_get_char_validated (text, -1);
138 return g_unichar_ispunct (uni);
141 static int
142 str_utf8_isalnum (const char *text)
144 gunichar uni = g_utf8_get_char_validated (text, -1);
145 return g_unichar_isalnum (uni);
148 static int
149 str_utf8_isdigit (const char *text)
151 gunichar uni = g_utf8_get_char_validated (text, -1);
152 return g_unichar_isdigit (uni);
155 static int
156 str_utf8_isprint (const char *ch)
158 gunichar uni = g_utf8_get_char_validated (ch, -1);
159 return g_unichar_isprint (uni);
162 static int
163 str_utf8_iscombiningmark (const char *ch)
165 gunichar uni = g_utf8_get_char_validated (ch, -1);
166 return str_unichar_iscombiningmark (uni);
169 static int
170 str_utf8_cnext_noncomb_char (const char **text)
172 int count = 0;
173 while ((*text)[0] != '\0')
175 str_utf8_cnext_char_safe (text);
176 count++;
177 if (!str_utf8_iscombiningmark (*text))
178 break;
180 return count;
183 static int
184 str_utf8_cprev_noncomb_char (const char **text, const char *begin)
186 int count = 0;
187 while ((*text) != begin)
189 str_utf8_cprev_char_safe (text);
190 count++;
191 if (!str_utf8_iscombiningmark (*text))
192 break;
194 return count;
197 static int
198 str_utf8_toupper (const char *text, char **out, size_t * remain)
200 gunichar uni;
201 size_t left;
203 uni = g_utf8_get_char_validated (text, -1);
204 if (uni == (gunichar) (-1) || uni == (gunichar) (-2))
205 return 0;
207 uni = g_unichar_toupper (uni);
208 left = g_unichar_to_utf8 (uni, NULL);
209 if (left >= *remain)
210 return 0;
212 left = g_unichar_to_utf8 (uni, *out);
213 (*out) += left;
214 (*remain) -= left;
215 return 1;
218 static int
219 str_utf8_tolower (const char *text, char **out, size_t * remain)
221 gunichar uni;
222 size_t left;
224 uni = g_utf8_get_char_validated (text, -1);
225 if (uni == (gunichar) (-1) || uni == (gunichar) (-2))
226 return 0;
228 uni = g_unichar_tolower (uni);
229 left = g_unichar_to_utf8 (uni, NULL);
230 if (left >= *remain)
231 return 0;
233 left = g_unichar_to_utf8 (uni, *out);
234 (*out) += left;
235 (*remain) -= left;
236 return 1;
239 static int
240 str_utf8_length (const char *text)
242 int result = 0;
243 const char *start;
244 const char *end;
246 start = text;
247 while (!g_utf8_validate (start, -1, &end) && start[0] != '\0')
249 if (start != end)
251 result += g_utf8_strlen (start, end - start);
253 result++;
254 start = end + 1;
257 if (start == text)
259 result = g_utf8_strlen (text, -1);
261 else
263 if (start[0] != '\0' && start != end)
265 result += g_utf8_strlen (start, end - start);
269 return result;
272 static int
273 str_utf8_length2 (const char *text, int size)
275 int result = 0;
276 const char *start;
277 const char *end;
279 start = text;
280 while (!g_utf8_validate (start, -1, &end) && start[0] != '\0' && size > 0)
282 if (start != end)
284 result += g_utf8_strlen (start, min (end - start, size));
285 size -= end - start;
287 result += (size > 0);
288 size--;
289 start = end + 1;
292 if (start == text)
294 result = g_utf8_strlen (text, size);
296 else
298 if (start[0] != '\0' && start != end && size > 0)
300 result += g_utf8_strlen (start, min (end - start, size));
304 return result;
307 static int
308 str_utf8_length_noncomb (const char *text)
310 int result = 0;
311 const char *t = text;
313 while (t[0] != '\0')
315 str_utf8_cnext_noncomb_char (&t);
316 result++;
319 return result;
323 static void
324 str_utf8_questmark_sustb (char **string, size_t * left, GString * buffer)
326 char *next = g_utf8_next_char (*string);
327 (*left) -= next - (*string);
328 (*string) = next;
329 g_string_append_c (buffer, '?');
333 static gchar *
334 str_utf8_conv_gerror_message (GError * error, const char *def_msg)
336 if ((error != NULL) && (error->message != NULL))
337 return g_strdup (error->message);
339 return g_strdup (def_msg != NULL ? def_msg : "");
342 static estr_t
343 str_utf8_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
345 estr_t result;
347 if (coder == str_cnv_not_convert)
349 g_string_append_len (buffer, string, size);
350 result = ESTR_SUCCESS;
352 else
353 result = str_nconvert (coder, (char *) string, size, buffer);
355 return result;
358 struct term_form
360 char text[BUF_MEDIUM * 6];
361 size_t width;
362 int compose;
365 /* utiliti function, that make string valid in utf8 and all characters printable
366 * return width of string too*/
367 static const struct term_form *
368 str_utf8_make_make_term_form (const char *text, size_t length)
370 static struct term_form result;
371 gunichar uni;
372 size_t left;
373 char *actual;
375 result.text[0] = '\0';
376 result.width = 0;
377 result.compose = 0;
378 actual = result.text;
380 /* check if text start with combining character,
381 * add space at begin in this case */
382 if (length != 0 && text[0] != '\0')
384 uni = g_utf8_get_char_validated (text, -1);
385 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
387 if (str_unichar_iscombiningmark (uni))
389 actual[0] = ' ';
390 actual++;
391 result.width++;
392 result.compose = 1;
397 while (length != 0 && text[0] != '\0')
399 uni = g_utf8_get_char_validated (text, -1);
400 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
402 if (g_unichar_isprint (uni))
404 left = g_unichar_to_utf8 (uni, actual);
405 actual += left;
406 if (!str_unichar_iscombiningmark (uni))
408 result.width++;
409 if (g_unichar_iswide (uni))
410 result.width++;
412 else
413 result.compose = 1;
415 else
417 actual[0] = '.';
418 actual++;
419 result.width++;
421 text = g_utf8_next_char (text);
423 else
425 text++;
426 /*actual[0] = '?'; */
427 memcpy (actual, replch, strlen (replch));
428 actual += strlen (replch);
429 result.width++;
431 if (length != (size_t) (-1))
432 length--;
434 actual[0] = '\0';
436 return &result;
439 static const char *
440 str_utf8_term_form (const char *text)
442 static char result[BUF_MEDIUM * 6];
443 const struct term_form *pre_form;
444 char *composed;
446 pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
447 if (pre_form->compose)
449 composed = g_utf8_normalize (pre_form->text, -1, G_NORMALIZE_DEFAULT_COMPOSE);
450 g_strlcpy (result, composed, sizeof (result));
451 g_free (composed);
453 else
455 g_strlcpy (result, pre_form->text, sizeof (result));
457 return result;
460 struct utf8_tool
462 char *actual;
463 size_t remain;
464 const char *cheked;
465 int ident;
466 int compose;
469 /* utiliti function, that copy all characters from cheked to actual */
470 static int
471 utf8_tool_copy_chars_to_end (struct utf8_tool *tool)
473 size_t left;
474 gunichar uni;
476 tool->compose = 0;
478 while (tool->cheked[0] != '\0')
480 uni = g_utf8_get_char (tool->cheked);
481 tool->compose |= str_unichar_iscombiningmark (uni);
482 left = g_unichar_to_utf8 (uni, NULL);
483 if (tool->remain <= left)
484 return 0;
485 left = g_unichar_to_utf8 (uni, tool->actual);
486 tool->actual += left;
487 tool->remain -= left;
488 tool->cheked = g_utf8_next_char (tool->cheked);
490 return 1;
493 /* utiliti function, that copy characters from cheked to actual until ident is
494 * smaller than to_ident */
495 static int
496 utf8_tool_copy_chars_to (struct utf8_tool *tool, int to_ident)
498 size_t left;
499 gunichar uni;
500 int w;
502 tool->compose = 0;
504 while (tool->cheked[0] != '\0')
506 uni = g_utf8_get_char (tool->cheked);
507 if (!str_unichar_iscombiningmark (uni))
509 w = 1;
510 if (g_unichar_iswide (uni))
511 w++;
512 if (tool->ident + w > to_ident)
513 return 1;
515 else
517 w = 0;
518 tool->compose = 1;
521 left = g_unichar_to_utf8 (uni, NULL);
522 if (tool->remain <= left)
523 return 0;
524 left = g_unichar_to_utf8 (uni, tool->actual);
525 tool->actual += left;
526 tool->remain -= left;
527 tool->cheked = g_utf8_next_char (tool->cheked);
528 tool->ident += w;
530 return 1;
533 /* utiliti function, add count spaces to actual */
534 static int
535 utf8_tool_insert_space (struct utf8_tool *tool, int count)
537 if (count <= 0)
538 return 1;
539 if (tool->remain <= (gsize) count)
540 return 0;
541 memset (tool->actual, ' ', count);
542 tool->actual += count;
543 tool->remain -= count;
544 return 1;
547 /* utiliti function, add one characters to actual */
548 static int
549 utf8_tool_insert_char (struct utf8_tool *tool, char ch)
551 if (tool->remain <= 1)
552 return 0;
553 tool->actual[0] = ch;
554 tool->actual++;
555 tool->remain--;
556 return 1;
559 /* utiliti function, thah skip characters from cheked until ident is greater or
560 * equal to to_ident */
561 static int
562 utf8_tool_skip_chars_to (struct utf8_tool *tool, int to_ident)
564 gunichar uni;
566 while (to_ident > tool->ident && tool->cheked[0] != '\0')
568 uni = g_utf8_get_char (tool->cheked);
569 if (!str_unichar_iscombiningmark (uni))
571 tool->ident++;
572 if (g_unichar_iswide (uni))
573 tool->ident++;
575 tool->cheked = g_utf8_next_char (tool->cheked);
577 uni = g_utf8_get_char (tool->cheked);
578 while (str_unichar_iscombiningmark (uni))
580 tool->cheked = g_utf8_next_char (tool->cheked);
581 uni = g_utf8_get_char (tool->cheked);
583 return 1;
586 static void
587 utf8_tool_compose (char *buffer, size_t size)
589 char *composed = g_utf8_normalize (buffer, -1, G_NORMALIZE_DEFAULT_COMPOSE);
590 g_strlcpy (buffer, composed, size);
591 g_free (composed);
595 static const char *
596 str_utf8_fit_to_term (const char *text, int width, align_crt_t just_mode)
598 static char result[BUF_MEDIUM * 6];
599 const struct term_form *pre_form;
600 struct utf8_tool tool;
602 pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
603 tool.cheked = pre_form->text;
604 tool.actual = result;
605 tool.remain = sizeof (result);
606 tool.compose = 0;
608 if (pre_form->width <= (gsize) width)
610 tool.ident = 0;
611 switch (HIDE_FIT (just_mode))
613 case J_CENTER_LEFT:
614 case J_CENTER:
615 tool.ident = (width - pre_form->width) / 2;
616 break;
617 case J_RIGHT:
618 tool.ident = width - pre_form->width;
619 break;
622 utf8_tool_insert_space (&tool, tool.ident);
623 utf8_tool_copy_chars_to_end (&tool);
624 utf8_tool_insert_space (&tool, width - pre_form->width - tool.ident);
626 else
628 if (IS_FIT (just_mode))
630 tool.ident = 0;
631 utf8_tool_copy_chars_to (&tool, width / 2);
632 utf8_tool_insert_char (&tool, '~');
634 tool.ident = 0;
635 utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1);
636 utf8_tool_copy_chars_to_end (&tool);
637 utf8_tool_insert_space (&tool, width - (pre_form->width - tool.ident + 1));
639 else
641 tool.ident = 0;
642 switch (HIDE_FIT (just_mode))
644 case J_CENTER:
645 tool.ident = (width - pre_form->width) / 2;
646 break;
647 case J_RIGHT:
648 tool.ident = width - pre_form->width;
649 break;
652 utf8_tool_skip_chars_to (&tool, 0);
653 utf8_tool_insert_space (&tool, tool.ident);
654 utf8_tool_copy_chars_to (&tool, width);
655 utf8_tool_insert_space (&tool, width - tool.ident);
659 tool.actual[0] = '\0';
660 if (tool.compose)
661 utf8_tool_compose (result, sizeof (result));
662 return result;
665 static const char *
666 str_utf8_term_trim (const char *text, int width)
668 static char result[BUF_MEDIUM * 6];
669 const struct term_form *pre_form;
670 struct utf8_tool tool;
672 pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
674 tool.cheked = pre_form->text;
675 tool.actual = result;
676 tool.remain = sizeof (result);
677 tool.compose = 0;
679 if ((gsize) width < pre_form->width)
681 if (width <= 3)
683 memset (tool.actual, '.', width);
684 tool.actual += width;
685 tool.remain -= width;
687 else
689 memset (tool.actual, '.', 3);
690 tool.actual += 3;
691 tool.remain -= 3;
693 tool.ident = 0;
694 utf8_tool_skip_chars_to (&tool, pre_form->width - width + 3);
695 utf8_tool_copy_chars_to_end (&tool);
698 else
700 utf8_tool_copy_chars_to_end (&tool);
703 tool.actual[0] = '\0';
704 if (tool.compose)
705 utf8_tool_compose (result, sizeof (result));
706 return result;
709 static int
710 str_utf8_term_width2 (const char *text, size_t length)
712 const struct term_form *result;
714 result = str_utf8_make_make_term_form (text, length);
715 return result->width;
718 static int
719 str_utf8_term_width1 (const char *text)
721 return str_utf8_term_width2 (text, (size_t) (-1));
724 static int
725 str_utf8_term_char_width (const char *text)
727 gunichar uni = g_utf8_get_char_validated (text, -1);
728 return (str_unichar_iscombiningmark (uni)) ? 0 : ((g_unichar_iswide (uni)) ? 2 : 1);
731 static const char *
732 str_utf8_term_substring (const char *text, int start, int width)
734 static char result[BUF_MEDIUM * 6];
735 const struct term_form *pre_form;
736 struct utf8_tool tool;
738 pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
740 tool.cheked = pre_form->text;
741 tool.actual = result;
742 tool.remain = sizeof (result);
743 tool.compose = 0;
745 tool.ident = -start;
746 utf8_tool_skip_chars_to (&tool, 0);
747 if (tool.ident < 0)
748 tool.ident = 0;
749 utf8_tool_insert_space (&tool, tool.ident);
751 utf8_tool_copy_chars_to (&tool, width);
752 utf8_tool_insert_space (&tool, width - tool.ident);
754 tool.actual[0] = '\0';
755 if (tool.compose)
756 utf8_tool_compose (result, sizeof (result));
757 return result;
760 static const char *
761 str_utf8_trunc (const char *text, int width)
763 static char result[MC_MAXPATHLEN * 6 * 2];
764 const struct term_form *pre_form;
765 struct utf8_tool tool;
767 pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
769 tool.cheked = pre_form->text;
770 tool.actual = result;
771 tool.remain = sizeof (result);
772 tool.compose = 0;
774 if (pre_form->width > (gsize) width)
776 tool.ident = 0;
777 utf8_tool_copy_chars_to (&tool, width / 2);
778 utf8_tool_insert_char (&tool, '~');
780 tool.ident = 0;
781 utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1);
782 utf8_tool_copy_chars_to_end (&tool);
784 else
786 utf8_tool_copy_chars_to_end (&tool);
789 tool.actual[0] = '\0';
790 if (tool.compose)
791 utf8_tool_compose (result, sizeof (result));
792 return result;
795 static int
796 str_utf8_offset_to_pos (const char *text, size_t length)
798 if (str_utf8_is_valid_string (text))
799 return g_utf8_offset_to_pointer (text, length) - text;
800 else
802 int result;
803 GString *buffer = g_string_new (text);
805 str_utf8_fix_string (buffer->str);
806 result = g_utf8_offset_to_pointer (buffer->str, length) - buffer->str;
807 g_string_free (buffer, TRUE);
808 return result;
812 static int
813 str_utf8_column_to_pos (const char *text, size_t pos)
815 static int result;
816 gunichar uni;
817 int width;
819 width = 0;
820 result = 0;
822 while (text[0] != '\0')
824 uni = g_utf8_get_char_validated (text, 6);
825 if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
827 if (g_unichar_isprint (uni))
829 if (!str_unichar_iscombiningmark (uni))
831 width++;
832 if (g_unichar_iswide (uni))
833 width++;
836 else
838 width++;
840 text = g_utf8_next_char (text);
842 else
844 text++;
845 width++;
847 if ((gsize) width > pos)
848 return result;
850 result++;
853 return result;
856 static char *
857 str_utf8_create_search_needle (const char *needle, int case_sen)
859 if (needle != NULL)
861 if (case_sen)
863 return g_utf8_normalize (needle, -1, G_NORMALIZE_ALL);
865 else
867 char *fold = g_utf8_casefold (needle, -1);
868 char *result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL);
869 g_free (fold);
870 return result;
873 else
874 return NULL;
877 static void
878 str_utf8_release_search_needle (char *needle, int case_sen)
880 (void) case_sen;
881 if (needle != NULL)
882 g_free (needle);
885 static const char *
886 str_utf8_search_first (const char *text, const char *search, int case_sen)
888 char *fold_text;
889 char *deco_text;
890 const char *match;
891 const char *result = NULL;
892 const char *m;
894 fold_text = (case_sen) ? (char *) text : g_utf8_casefold (text, -1);
895 deco_text = g_utf8_normalize (fold_text, -1, G_NORMALIZE_ALL);
897 match = deco_text;
900 match = g_strstr_len (match, -1, search);
901 if (match != NULL)
903 if ((!str_utf8_iscombiningmark (match) || (match == deco_text)) &&
904 !str_utf8_iscombiningmark (match + strlen (search)))
907 result = text;
908 m = deco_text;
909 while (m < match)
911 str_utf8_cnext_noncomb_char (&m);
912 str_utf8_cnext_noncomb_char (&result);
915 else
917 str_utf8_cnext_char (&match);
921 while (match != NULL && result == NULL);
923 g_free (deco_text);
924 if (!case_sen)
925 g_free (fold_text);
927 return result;
930 static const char *
931 str_utf8_search_last (const char *text, const char *search, int case_sen)
933 char *fold_text;
934 char *deco_text;
935 char *match;
936 const char *result = NULL;
937 const char *m;
939 fold_text = (case_sen) ? (char *) text : g_utf8_casefold (text, -1);
940 deco_text = g_utf8_normalize (fold_text, -1, G_NORMALIZE_ALL);
944 match = g_strrstr_len (deco_text, -1, search);
945 if (match != NULL)
947 if ((!str_utf8_iscombiningmark (match) || (match == deco_text)) &&
948 !str_utf8_iscombiningmark (match + strlen (search)))
951 result = text;
952 m = deco_text;
953 while (m < match)
955 str_utf8_cnext_noncomb_char (&m);
956 str_utf8_cnext_noncomb_char (&result);
959 else
961 match[0] = '\0';
965 while (match != NULL && result == NULL);
967 g_free (deco_text);
968 if (!case_sen)
969 g_free (fold_text);
971 return result;
974 static char *
975 str_utf8_normalize (const char *text)
977 GString *fixed = g_string_new ("");
978 char *tmp;
979 char *result;
980 const char *start;
981 const char *end;
983 start = text;
984 while (!g_utf8_validate (start, -1, &end) && start[0] != '\0')
986 if (start != end)
988 tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL);
989 g_string_append (fixed, tmp);
990 g_free (tmp);
992 g_string_append_c (fixed, end[0]);
993 start = end + 1;
996 if (start == text)
998 result = g_utf8_normalize (text, -1, G_NORMALIZE_ALL);
1000 else
1002 if (start[0] != '\0' && start != end)
1004 tmp = g_utf8_normalize (start, end - start, G_NORMALIZE_ALL);
1005 g_string_append (fixed, tmp);
1006 g_free (tmp);
1008 result = g_strdup (fixed->str);
1010 g_string_free (fixed, TRUE);
1012 return result;
1015 static char *
1016 str_utf8_casefold_normalize (const char *text)
1018 GString *fixed = g_string_new ("");
1019 char *tmp, *fold;
1020 char *result;
1021 const char *start;
1022 const char *end;
1024 start = text;
1025 while (!g_utf8_validate (start, -1, &end) && start[0] != '\0')
1027 if (start != end)
1029 fold = g_utf8_casefold (start, end - start);
1030 tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL);
1031 g_string_append (fixed, tmp);
1032 g_free (tmp);
1033 g_free (fold);
1035 g_string_append_c (fixed, end[0]);
1036 start = end + 1;
1039 if (start == text)
1041 fold = g_utf8_casefold (text, -1);
1042 result = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL);
1043 g_free (fold);
1045 else
1047 if (start[0] != '\0' && start != end)
1049 fold = g_utf8_casefold (start, end - start);
1050 tmp = g_utf8_normalize (fold, -1, G_NORMALIZE_ALL);
1051 g_string_append (fixed, tmp);
1052 g_free (tmp);
1053 g_free (fold);
1055 result = g_strdup (fixed->str);
1057 g_string_free (fixed, TRUE);
1059 return result;
1062 static int
1063 str_utf8_compare (const char *t1, const char *t2)
1065 char *n1, *n2;
1066 int result;
1068 n1 = str_utf8_normalize (t1);
1069 n2 = str_utf8_normalize (t2);
1071 result = strcmp (n1, n2);
1073 g_free (n1);
1074 g_free (n2);
1076 return result;
1079 static int
1080 str_utf8_ncompare (const char *t1, const char *t2)
1082 char *n1, *n2;
1083 int result;
1085 n1 = str_utf8_normalize (t1);
1086 n2 = str_utf8_normalize (t2);
1088 result = strncmp (n1, n2, min (strlen (n1), strlen (n2)));
1090 g_free (n1);
1091 g_free (n2);
1093 return result;
1096 static int
1097 str_utf8_casecmp (const char *t1, const char *t2)
1099 char *n1, *n2;
1100 int result;
1102 n1 = str_utf8_casefold_normalize (t1);
1103 n2 = str_utf8_casefold_normalize (t2);
1105 result = strcmp (n1, n2);
1107 g_free (n1);
1108 g_free (n2);
1110 return result;
1113 static int
1114 str_utf8_ncasecmp (const char *t1, const char *t2)
1116 char *n1, *n2;
1117 int result;
1119 n1 = str_utf8_casefold_normalize (t1);
1120 n2 = str_utf8_casefold_normalize (t2);
1122 result = strncmp (n1, n2, min (strlen (n1), strlen (n2)));
1124 g_free (n1);
1125 g_free (n2);
1127 return result;
1130 static int
1131 str_utf8_prefix (const char *text, const char *prefix)
1133 char *t = str_utf8_normalize (text);
1134 char *p = str_utf8_normalize (prefix);
1135 const char *nt = t;
1136 const char *np = p;
1137 const char *nnt = t;
1138 const char *nnp = p;
1139 int result;
1141 while (nt[0] != '\0' && np[0] != '\0')
1143 str_utf8_cnext_char_safe (&nnt);
1144 str_utf8_cnext_char_safe (&nnp);
1145 if (nnt - nt != nnp - np)
1146 break;
1147 if (strncmp (nt, np, nnt - nt) != 0)
1148 break;
1149 nt = nnt;
1150 np = nnp;
1153 result = np - p;
1155 g_free (t);
1156 g_free (p);
1158 return result;
1161 static int
1162 str_utf8_caseprefix (const char *text, const char *prefix)
1164 char *t = str_utf8_casefold_normalize (text);
1165 char *p = str_utf8_casefold_normalize (prefix);
1166 const char *nt = t;
1167 const char *np = p;
1168 const char *nnt = t;
1169 const char *nnp = p;
1170 int result;
1172 while (nt[0] != '\0' && np[0] != '\0')
1174 str_utf8_cnext_char_safe (&nnt);
1175 str_utf8_cnext_char_safe (&nnp);
1176 if (nnt - nt != nnp - np)
1177 break;
1178 if (strncmp (nt, np, nnt - nt) != 0)
1179 break;
1180 nt = nnt;
1181 np = nnp;
1184 result = np - p;
1186 g_free (t);
1187 g_free (p);
1189 return result;
1192 static char *
1193 str_utf8_create_key_gen (const char *text, int case_sen,
1194 gchar * (*keygen) (const gchar * text, gssize size))
1196 char *result;
1198 if (case_sen)
1200 result = str_utf8_normalize (text);
1202 else
1204 gboolean dot;
1205 GString *fixed;
1206 const char *start, *end;
1207 char *fold, *key;
1209 dot = text[0] == '.';
1210 fixed = g_string_sized_new (16);
1212 if (!dot)
1213 start = text;
1214 else
1216 start = text + 1;
1217 g_string_append_c (fixed, '.');
1220 while (!g_utf8_validate (start, -1, &end) && start[0] != '\0')
1222 if (start != end)
1224 fold = g_utf8_casefold (start, end - start);
1225 key = keygen (fold, -1);
1226 g_string_append (fixed, key);
1227 g_free (key);
1228 g_free (fold);
1230 g_string_append_c (fixed, end[0]);
1231 start = end + 1;
1234 if (start == text)
1236 fold = g_utf8_casefold (start, -1);
1237 result = keygen (fold, -1);
1238 g_free (fold);
1239 g_string_free (fixed, TRUE);
1241 else if (dot && (start == text + 1))
1243 fold = g_utf8_casefold (start, -1);
1244 key = keygen (fold, -1);
1245 g_string_append (fixed, key);
1246 g_free (key);
1247 g_free (fold);
1248 result = g_string_free (fixed, FALSE);
1250 else
1252 if (start[0] != '\0' && start != end)
1254 fold = g_utf8_casefold (start, end - start);
1255 key = keygen (fold, -1);
1256 g_string_append (fixed, key);
1257 g_free (key);
1258 g_free (fold);
1260 result = g_string_free (fixed, FALSE);
1263 return result;
1266 static char *
1267 str_utf8_create_key (const char *text, int case_sen)
1269 return str_utf8_create_key_gen (text, case_sen, g_utf8_collate_key);
1272 #ifdef MC__USE_STR_UTF8_CREATE_KEY_FOR_FILENAME
1273 static char *
1274 str_utf8_create_key_for_filename (const char *text, int case_sen)
1276 return str_utf8_create_key_gen (text, case_sen, g_utf8_collate_key_for_filename);
1278 #endif
1280 static int
1281 str_utf8_key_collate (const char *t1, const char *t2, int case_sen)
1283 (void) case_sen;
1284 return strcmp (t1, t2);
1287 static void
1288 str_utf8_release_key (char *key, int case_sen)
1290 (void) case_sen;
1291 g_free (key);
1294 struct str_class
1295 str_utf8_init (void)
1297 struct str_class result;
1299 result.conv_gerror_message = str_utf8_conv_gerror_message;
1300 result.vfs_convert_to = str_utf8_vfs_convert_to;
1301 result.insert_replace_char = str_utf8_insert_replace_char;
1302 result.is_valid_string = str_utf8_is_valid_string;
1303 result.is_valid_char = str_utf8_is_valid_char;
1304 result.cnext_char = str_utf8_cnext_char;
1305 result.cprev_char = str_utf8_cprev_char;
1306 result.cnext_char_safe = str_utf8_cnext_char_safe;
1307 result.cprev_char_safe = str_utf8_cprev_char_safe;
1308 result.cnext_noncomb_char = str_utf8_cnext_noncomb_char;
1309 result.cprev_noncomb_char = str_utf8_cprev_noncomb_char;
1310 result.isspace = str_utf8_isspace;
1311 result.ispunct = str_utf8_ispunct;
1312 result.isalnum = str_utf8_isalnum;
1313 result.isdigit = str_utf8_isdigit;
1314 result.isprint = str_utf8_isprint;
1315 result.iscombiningmark = str_utf8_iscombiningmark;
1316 result.toupper = str_utf8_toupper;
1317 result.tolower = str_utf8_tolower;
1318 result.length = str_utf8_length;
1319 result.length2 = str_utf8_length2;
1320 result.length_noncomb = str_utf8_length_noncomb;
1321 result.fix_string = str_utf8_fix_string;
1322 result.term_form = str_utf8_term_form;
1323 result.fit_to_term = str_utf8_fit_to_term;
1324 result.term_trim = str_utf8_term_trim;
1325 result.term_width2 = str_utf8_term_width2;
1326 result.term_width1 = str_utf8_term_width1;
1327 result.term_char_width = str_utf8_term_char_width;
1328 result.term_substring = str_utf8_term_substring;
1329 result.trunc = str_utf8_trunc;
1330 result.offset_to_pos = str_utf8_offset_to_pos;
1331 result.column_to_pos = str_utf8_column_to_pos;
1332 result.create_search_needle = str_utf8_create_search_needle;
1333 result.release_search_needle = str_utf8_release_search_needle;
1334 result.search_first = str_utf8_search_first;
1335 result.search_last = str_utf8_search_last;
1336 result.compare = str_utf8_compare;
1337 result.ncompare = str_utf8_ncompare;
1338 result.casecmp = str_utf8_casecmp;
1339 result.ncasecmp = str_utf8_ncasecmp;
1340 result.prefix = str_utf8_prefix;
1341 result.caseprefix = str_utf8_caseprefix;
1342 result.create_key = str_utf8_create_key;
1343 #ifdef MC__USE_STR_UTF8_CREATE_KEY_FOR_FILENAME
1344 /* case insensitive sort files in "a1 a2 a10" order */
1345 result.create_key_for_filename = str_utf8_create_key_for_filename;
1346 #else
1347 /* case insensitive sort files in "a1 a10 a2" order */
1348 result.create_key_for_filename = str_utf8_create_key;
1349 #endif
1350 result.key_collate = str_utf8_key_collate;
1351 result.release_key = str_utf8_release_key;
1353 return result;