4 Copyright (C) 2007-2016
5 Free Software Foundation, Inc.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "lib/global.h"
32 #include "lib/strutil.h"
34 /* Functions for singlebyte encodings, all characters have width 1
35 * using standard system functions.
36 * There are only small differences between functions in strutil8bit.c
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
45 * Inlines to equalize 'char' signedness for single 'char' encodings.
47 * isspace ((unsigned char) c);
51 #define DECLARE_CTYPE_WRAPPER(func_name) \
52 static inline int char_##func_name(char c) \
54 return func_name((int)(unsigned char)c); \
57 /*** file scope type declarations ****************************************************************/
59 /*** file scope variables ************************************************************************/
61 static const char replch
= '?';
63 /* --------------------------------------------------------------------------------------------- */
64 /*** file scope functions ************************************************************************/
65 /* --------------------------------------------------------------------------------------------- */
68 DECLARE_CTYPE_WRAPPER (isalnum
)
69 DECLARE_CTYPE_WRAPPER (isdigit
)
70 DECLARE_CTYPE_WRAPPER (isprint
)
71 DECLARE_CTYPE_WRAPPER (ispunct
)
72 DECLARE_CTYPE_WRAPPER (isspace
)
73 DECLARE_CTYPE_WRAPPER (toupper
)
74 DECLARE_CTYPE_WRAPPER (tolower
)
77 /* --------------------------------------------------------------------------------------------- */
80 str_8bit_insert_replace_char (GString
* buffer
)
82 g_string_append_c (buffer
, replch
);
85 /* --------------------------------------------------------------------------------------------- */
88 str_8bit_is_valid_string (const char *text
)
94 /* --------------------------------------------------------------------------------------------- */
97 str_8bit_is_valid_char (const char *ch
, size_t size
)
104 /* --------------------------------------------------------------------------------------------- */
107 str_8bit_cnext_char (const char **text
)
112 /* --------------------------------------------------------------------------------------------- */
115 str_8bit_cprev_char (const char **text
)
120 /* --------------------------------------------------------------------------------------------- */
123 str_8bit_cnext_noncomb_char (const char **text
)
125 if (*text
[0] == '\0')
132 /* --------------------------------------------------------------------------------------------- */
135 str_8bit_cprev_noncomb_char (const char **text
, const char *begin
)
137 if ((*text
) == begin
)
144 /* --------------------------------------------------------------------------------------------- */
147 str_8bit_isspace (const char *text
)
149 return char_isspace (text
[0]);
152 /* --------------------------------------------------------------------------------------------- */
155 str_8bit_ispunct (const char *text
)
157 return char_ispunct (text
[0]);
160 /* --------------------------------------------------------------------------------------------- */
163 str_8bit_isalnum (const char *text
)
165 return char_isalnum (text
[0]);
168 /* --------------------------------------------------------------------------------------------- */
171 str_8bit_isdigit (const char *text
)
173 return char_isdigit (text
[0]);
176 /* --------------------------------------------------------------------------------------------- */
179 str_8bit_isprint (const char *text
)
181 return char_isprint (text
[0]);
184 /* --------------------------------------------------------------------------------------------- */
187 str_8bit_iscombiningmark (const char *text
)
193 /* --------------------------------------------------------------------------------------------- */
196 str_8bit_toupper (const char *text
, char **out
, size_t * remain
)
201 (*out
)[0] = char_toupper (text
[0]);
207 /* --------------------------------------------------------------------------------------------- */
210 str_8bit_tolower (const char *text
, char **out
, size_t * remain
)
215 (*out
)[0] = char_tolower (text
[0]);
221 /* --------------------------------------------------------------------------------------------- */
224 str_8bit_length (const char *text
)
226 return strlen (text
);
229 /* --------------------------------------------------------------------------------------------- */
232 str_8bit_length2 (const char *text
, int size
)
234 return (size
>= 0) ? MIN (strlen (text
), (gsize
) size
) : strlen (text
);
237 /* --------------------------------------------------------------------------------------------- */
240 str_8bit_conv_gerror_message (GError
* mcerror
, const char *def_msg
)
245 /* glib messages are in UTF-8 charset */
246 conv
= str_crt_conv_from ("UTF-8");
248 if (conv
== INVALID_CONV
)
249 ret
= g_strdup (def_msg
!= NULL
? def_msg
: "");
254 buf
= g_string_new ("");
256 if (str_convert (conv
, mcerror
->message
, buf
) != ESTR_FAILURE
)
257 ret
= g_string_free (buf
, FALSE
);
260 ret
= g_strdup (def_msg
!= NULL
? def_msg
: "");
261 g_string_free (buf
, TRUE
);
264 str_close_conv (conv
);
270 /* --------------------------------------------------------------------------------------------- */
273 str_8bit_vfs_convert_to (GIConv coder
, const char *string
, int size
, GString
* buffer
)
275 estr_t result
= ESTR_SUCCESS
;
277 if (coder
== str_cnv_not_convert
)
278 g_string_append_len (buffer
, string
, size
);
280 result
= str_nconvert (coder
, string
, size
, buffer
);
285 /* --------------------------------------------------------------------------------------------- */
288 str_8bit_term_form (const char *text
)
290 static char result
[BUF_MEDIUM
];
297 remain
= sizeof (result
);
298 length
= strlen (text
);
300 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
301 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
307 /* --------------------------------------------------------------------------------------------- */
310 str_8bit_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
312 static char result
[BUF_MEDIUM
];
319 length
= strlen (text
);
321 remain
= sizeof (result
);
323 if ((int) length
<= width
)
325 switch (HIDE_FIT (just_mode
))
329 ident
= (width
- length
) / 2;
332 ident
= width
- length
;
338 if ((int) remain
<= ident
)
340 memset (actual
, ' ', ident
);
344 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
345 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
347 if (width
- length
- ident
> 0)
349 if (remain
<= width
- length
- ident
)
351 memset (actual
, ' ', width
- length
- ident
);
352 actual
+= width
- length
- ident
;
355 else if (IS_FIT (just_mode
))
357 for (; pos
+ 1 <= (gsize
) width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
358 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
366 pos
+= length
- width
+ 1;
367 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
368 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
372 switch (HIDE_FIT (just_mode
))
375 ident
= (length
- width
) / 2;
378 ident
= length
- width
;
385 for (; pos
< (gsize
) (ident
+ width
) && remain
> 1; pos
++, actual
++, remain
--)
386 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
390 if (actual
>= result
+ sizeof (result
))
391 actual
= result
+ sizeof (result
) - 1;
396 /* --------------------------------------------------------------------------------------------- */
399 str_8bit_term_trim (const char *text
, int width
)
401 static char result
[BUF_MEDIUM
];
406 length
= strlen (text
);
408 remain
= sizeof (result
);
414 if (width
>= (int) length
)
416 for (pos
= 0; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
417 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
421 memset (actual
, '.', width
);
426 memset (actual
, '.', 3);
430 for (pos
= length
- width
+ 3; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
431 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
439 /* --------------------------------------------------------------------------------------------- */
442 str_8bit_term_width2 (const char *text
, size_t length
)
444 return (length
!= (size_t) (-1)) ? MIN (strlen (text
), length
) : strlen (text
);
447 /* --------------------------------------------------------------------------------------------- */
450 str_8bit_term_width1 (const char *text
)
452 return str_8bit_term_width2 (text
, (size_t) (-1));
455 /* --------------------------------------------------------------------------------------------- */
458 str_8bit_term_char_width (const char *text
)
464 /* --------------------------------------------------------------------------------------------- */
467 str_8bit_term_substring (const char *text
, int start
, int width
)
469 static char result
[BUF_MEDIUM
];
475 remain
= sizeof (result
);
476 length
= strlen (text
);
478 if (start
< (int) length
)
482 for (pos
= start
; pos
< length
&& width
> 0 && remain
> 1;
483 pos
++, width
--, actual
++, remain
--)
484 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
487 for (; width
> 0 && remain
> 1; actual
++, remain
--, width
--)
494 /* --------------------------------------------------------------------------------------------- */
497 str_8bit_trunc (const char *text
, int width
)
499 static char result
[MC_MAXPATHLEN
];
506 remain
= sizeof (result
);
507 length
= strlen (text
);
509 if ((int) length
> width
)
511 for (; pos
+ 1 <= (gsize
) width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
512 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
520 pos
+= length
- width
+ 1;
521 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
522 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
526 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
527 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
535 /* --------------------------------------------------------------------------------------------- */
538 str_8bit_offset_to_pos (const char *text
, size_t length
)
544 /* --------------------------------------------------------------------------------------------- */
547 str_8bit_column_to_pos (const char *text
, size_t pos
)
553 /* --------------------------------------------------------------------------------------------- */
556 str_8bit_create_search_needle (const char *needle
, int case_sen
)
559 return (char *) needle
;
562 /* --------------------------------------------------------------------------------------------- */
565 str_8bit_release_search_needle (char *needle
, int case_sen
)
571 /* --------------------------------------------------------------------------------------------- */
574 str_8bit_strdown (const char *str
)
581 rets
= g_strdup (str
);
583 for (p
= rets
; *p
!= '\0'; p
++)
584 *p
= char_tolower (*p
);
589 /* --------------------------------------------------------------------------------------------- */
592 str_8bit_search_first (const char *text
, const char *search
, int case_sen
)
598 fold_text
= (case_sen
) ? (char *) text
: str_8bit_strdown (text
);
599 fold_search
= (case_sen
) ? (char *) search
: str_8bit_strdown (search
);
601 match
= g_strstr_len (fold_text
, -1, fold_search
);
606 offset
= match
- fold_text
;
607 match
= text
+ offset
;
613 g_free (fold_search
);
619 /* --------------------------------------------------------------------------------------------- */
622 str_8bit_search_last (const char *text
, const char *search
, int case_sen
)
628 fold_text
= (case_sen
) ? (char *) text
: str_8bit_strdown (text
);
629 fold_search
= (case_sen
) ? (char *) search
: str_8bit_strdown (search
);
631 match
= g_strrstr_len (fold_text
, -1, fold_search
);
636 offset
= match
- fold_text
;
637 match
= text
+ offset
;
643 g_free (fold_search
);
649 /* --------------------------------------------------------------------------------------------- */
652 str_8bit_compare (const char *t1
, const char *t2
)
654 return strcmp (t1
, t2
);
657 /* --------------------------------------------------------------------------------------------- */
660 str_8bit_ncompare (const char *t1
, const char *t2
)
662 return strncmp (t1
, t2
, MIN (strlen (t1
), strlen (t2
)));
665 /* --------------------------------------------------------------------------------------------- */
668 str_8bit_casecmp (const char *s1
, const char *s2
)
672 #ifdef HAVE_STRCASECMP
673 g_return_val_if_fail (s1
!= NULL
, 0);
674 g_return_val_if_fail (s2
!= NULL
, 0);
676 return strcasecmp (s1
, s2
);
680 g_return_val_if_fail (s1
!= NULL
, 0);
681 g_return_val_if_fail (s2
!= NULL
, 0);
683 while (*s1
!= '\0' && *s2
!= '\0')
685 /* According to A. Cox, some platforms have islower's that
686 * don't work right on non-uppercase
688 c1
= isupper ((guchar
) * s1
) ? tolower ((guchar
) * s1
) : *s1
;
689 c2
= isupper ((guchar
) * s2
) ? tolower ((guchar
) * s2
) : *s2
;
696 return (((gint
) (guchar
) * s1
) - ((gint
) (guchar
) * s2
));
700 /* --------------------------------------------------------------------------------------------- */
703 str_8bit_ncasecmp (const char *s1
, const char *s2
)
707 g_return_val_if_fail (s1
!= NULL
, 0);
708 g_return_val_if_fail (s2
!= NULL
, 0);
710 n
= MIN (strlen (s1
), strlen (s2
));
714 #ifdef HAVE_STRNCASECMP
715 return strncasecmp (s1
, s2
, n
);
719 while (n
!= 0 && *s1
!= '\0' && *s2
!= '\0')
722 /* According to A. Cox, some platforms have islower's that
723 * don't work right on non-uppercase
725 c1
= isupper ((guchar
) * s1
) ? tolower ((guchar
) * s1
) : *s1
;
726 c2
= isupper ((guchar
) * s2
) ? tolower ((guchar
) * s2
) : *s2
;
736 return (((gint
) (guchar
) * s1
) - ((gint
) (guchar
) * s2
));
741 /* --------------------------------------------------------------------------------------------- */
744 str_8bit_prefix (const char *text
, const char *prefix
)
748 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
749 && text
[result
] == prefix
[result
]; result
++);
754 /* --------------------------------------------------------------------------------------------- */
757 str_8bit_caseprefix (const char *text
, const char *prefix
)
761 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
762 && char_toupper (text
[result
]) == char_toupper (prefix
[result
]); result
++);
767 /* --------------------------------------------------------------------------------------------- */
770 str_8bit_fix_string (char *text
)
775 /* --------------------------------------------------------------------------------------------- */
778 str_8bit_create_key (const char *text
, int case_sen
)
780 return (case_sen
) ? (char *) text
: str_8bit_strdown (text
);
783 /* --------------------------------------------------------------------------------------------- */
786 str_8bit_key_collate (const char *t1
, const char *t2
, int case_sen
)
789 return strcmp (t1
, t2
);
791 return strcoll (t1
, t2
);
794 /* --------------------------------------------------------------------------------------------- */
797 str_8bit_release_key (char *key
, int case_sen
)
803 /* --------------------------------------------------------------------------------------------- */
804 /*** public functions ****************************************************************************/
805 /* --------------------------------------------------------------------------------------------- */
810 struct str_class result
;
812 result
.conv_gerror_message
= str_8bit_conv_gerror_message
;
813 result
.vfs_convert_to
= str_8bit_vfs_convert_to
;
814 result
.insert_replace_char
= str_8bit_insert_replace_char
;
815 result
.is_valid_string
= str_8bit_is_valid_string
;
816 result
.is_valid_char
= str_8bit_is_valid_char
;
817 result
.cnext_char
= str_8bit_cnext_char
;
818 result
.cprev_char
= str_8bit_cprev_char
;
819 result
.cnext_char_safe
= str_8bit_cnext_char
;
820 result
.cprev_char_safe
= str_8bit_cprev_char
;
821 result
.cnext_noncomb_char
= str_8bit_cnext_noncomb_char
;
822 result
.cprev_noncomb_char
= str_8bit_cprev_noncomb_char
;
823 result
.char_isspace
= str_8bit_isspace
;
824 result
.char_ispunct
= str_8bit_ispunct
;
825 result
.char_isalnum
= str_8bit_isalnum
;
826 result
.char_isdigit
= str_8bit_isdigit
;
827 result
.char_isprint
= str_8bit_isprint
;
828 result
.char_iscombiningmark
= str_8bit_iscombiningmark
;
829 result
.char_toupper
= str_8bit_toupper
;
830 result
.char_tolower
= str_8bit_tolower
;
831 result
.length
= str_8bit_length
;
832 result
.length2
= str_8bit_length2
;
833 result
.length_noncomb
= str_8bit_length
;
834 result
.fix_string
= str_8bit_fix_string
;
835 result
.term_form
= str_8bit_term_form
;
836 result
.fit_to_term
= str_8bit_fit_to_term
;
837 result
.term_trim
= str_8bit_term_trim
;
838 result
.term_width2
= str_8bit_term_width2
;
839 result
.term_width1
= str_8bit_term_width1
;
840 result
.term_char_width
= str_8bit_term_char_width
;
841 result
.term_substring
= str_8bit_term_substring
;
842 result
.trunc
= str_8bit_trunc
;
843 result
.offset_to_pos
= str_8bit_offset_to_pos
;
844 result
.column_to_pos
= str_8bit_column_to_pos
;
845 result
.create_search_needle
= str_8bit_create_search_needle
;
846 result
.release_search_needle
= str_8bit_release_search_needle
;
847 result
.search_first
= str_8bit_search_first
;
848 result
.search_last
= str_8bit_search_last
;
849 result
.compare
= str_8bit_compare
;
850 result
.ncompare
= str_8bit_ncompare
;
851 result
.casecmp
= str_8bit_casecmp
;
852 result
.ncasecmp
= str_8bit_ncasecmp
;
853 result
.prefix
= str_8bit_prefix
;
854 result
.caseprefix
= str_8bit_caseprefix
;
855 result
.create_key
= str_8bit_create_key
;
856 result
.create_key_for_filename
= str_8bit_create_key
;
857 result
.key_collate
= str_8bit_key_collate
;
858 result
.release_key
= str_8bit_release_key
;
863 /* --------------------------------------------------------------------------------------------- */