1 /* 8bit strings utilities
2 Copyright (C) 2007 Free Software Foundation, Inc.
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.
30 #include "lib/global.h"
31 #include "lib/strutil.h"
33 /* functions for singlebyte encodings, all characters have width 1
34 * using standard system functions
35 * there are only small differences between functions in strutil8bit.c
39 static const char replch
= '?';
42 * Inlines to equalize 'char' signedness for single 'char' encodings.
44 * isspace((unsigned char)c);
49 #define DECLARE_CTYPE_WRAPPER(func_name) \
50 static inline int char_##func_name(char c) \
52 return func_name((int)(unsigned char)c); \
55 DECLARE_CTYPE_WRAPPER(isalnum
)
56 DECLARE_CTYPE_WRAPPER(isalpha
)
57 DECLARE_CTYPE_WRAPPER(isascii
)
58 DECLARE_CTYPE_WRAPPER(isblank
)
59 DECLARE_CTYPE_WRAPPER(iscntrl
)
60 DECLARE_CTYPE_WRAPPER(isdigit
)
61 DECLARE_CTYPE_WRAPPER(isgraph
)
62 DECLARE_CTYPE_WRAPPER(islower
)
63 DECLARE_CTYPE_WRAPPER(isprint
)
64 DECLARE_CTYPE_WRAPPER(ispunct
)
65 DECLARE_CTYPE_WRAPPER(isspace
)
66 DECLARE_CTYPE_WRAPPER(isupper
)
67 DECLARE_CTYPE_WRAPPER(isxdigit
)
68 DECLARE_CTYPE_WRAPPER(toupper
)
69 DECLARE_CTYPE_WRAPPER(tolower
)
72 str_8bit_insert_replace_char (GString
* buffer
)
74 g_string_append_c (buffer
, replch
);
78 str_8bit_is_valid_string (const char *text
)
85 str_8bit_is_valid_char (const char *ch
, size_t size
)
93 str_8bit_cnext_char (const char **text
)
99 str_8bit_cprev_char (const char **text
)
105 str_8bit_cnext_noncomb_char (const char **text
)
107 if (*text
[0] != '\0')
117 str_8bit_cprev_noncomb_char (const char **text
, const char *begin
)
119 if ((*text
) != begin
)
129 str_8bit_isspace (const char *text
)
131 return char_isspace (text
[0]);
135 str_8bit_ispunct (const char *text
)
137 return char_ispunct (text
[0]);
141 str_8bit_isalnum (const char *text
)
143 return char_isalnum (text
[0]);
147 str_8bit_isdigit (const char *text
)
149 return char_isdigit (text
[0]);
153 str_8bit_isprint (const char *text
)
155 return char_isprint (text
[0]);
159 str_8bit_iscombiningmark (const char *text
)
166 str_8bit_toupper (const char *text
, char **out
, size_t * remain
)
170 (*out
)[0] = char_toupper (text
[0]);
177 str_8bit_tolower (const char *text
, char **out
, size_t * remain
)
181 (*out
)[0] = char_tolower (text
[0]);
188 str_8bit_length (const char *text
)
190 return strlen (text
);
194 str_8bit_length2 (const char *text
, int size
)
196 return (size
>= 0) ? min (strlen (text
), (gsize
)size
) : strlen (text
);
200 str_8bit_conv_gerror_message (GError
*error
, const char *def_msg
)
205 /* glib messages are in UTF-8 charset */
206 conv
= str_crt_conv_from ("UTF-8");
208 if (conv
== INVALID_CONV
)
209 ret
= g_strdup (def_msg
!= NULL
? def_msg
: "");
213 buf
= g_string_new ("");
215 if (str_convert (conv
, error
->message
, buf
) != ESTR_FAILURE
) {
217 g_string_free (buf
, FALSE
);
219 ret
= g_strdup (def_msg
!= NULL
? def_msg
: "");
220 g_string_free (buf
, TRUE
);
223 str_close_conv (conv
);
230 str_8bit_vfs_convert_to (GIConv coder
, const char *string
,
231 int size
, GString
* buffer
)
235 if (coder
== str_cnv_not_convert
)
237 g_string_append_len (buffer
, string
, size
);
238 result
= ESTR_SUCCESS
;
241 result
= str_nconvert (coder
, (char *) string
, size
, buffer
);
248 str_8bit_term_form (const char *text
)
250 static char result
[BUF_MEDIUM
];
257 remain
= sizeof (result
);
258 length
= strlen (text
);
260 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
262 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
270 str_8bit_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
272 static char result
[BUF_MEDIUM
];
279 length
= strlen (text
);
281 remain
= sizeof (result
);
283 if ((int)length
<= width
)
286 switch (HIDE_FIT (just_mode
))
290 ident
= (width
- length
) / 2;
293 ident
= width
- length
;
297 if ((int)remain
<= ident
)
299 memset (actual
, ' ', ident
);
303 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
305 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
307 if (width
- length
- ident
> 0)
309 if (remain
<= width
- length
- ident
)
311 memset (actual
, ' ', width
- length
- ident
);
312 actual
+= width
- length
- ident
;
313 remain
-= width
- length
- ident
;
318 if (IS_FIT (just_mode
))
320 for (; pos
+ 1 <= (gsize
)width
/ 2 && remain
> 1;
321 actual
++, pos
++, remain
--)
324 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
333 pos
+= length
- width
+ 1;
335 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
337 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
343 switch (HIDE_FIT (just_mode
))
346 ident
= (length
- width
) / 2;
349 ident
= length
- width
;
354 for (; pos
< (gsize
)(ident
+ width
) && remain
> 1;
355 pos
++, actual
++, remain
--)
358 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
369 str_8bit_term_trim (const char *text
, int width
)
371 static char result
[BUF_MEDIUM
];
377 length
= strlen (text
);
379 remain
= sizeof (result
);
381 if (width
< (int)length
)
385 memset (actual
, '.', width
);
391 memset (actual
, '.', 3);
395 pos
+= length
- width
+ 3;
397 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
399 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
405 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
407 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
416 str_8bit_term_width2 (const char *text
, size_t length
)
418 return (length
!= (size_t) (-1))
419 ? min (strlen (text
), length
) : strlen (text
);
423 str_8bit_term_width1 (const char *text
)
425 return str_8bit_term_width2 (text
, (size_t) (-1));
429 str_8bit_term_char_width (const char *text
)
436 str_8bit_msg_term_size (const char *text
, int *lines
, int *columns
)
446 tmp
= g_strdup ((char *)text
);
450 q
= strchr (p
, '\n');
457 width
= str_8bit_term_width1 (p
);
458 if (width
> (*columns
))
471 str_8bit_term_substring (const char *text
, int start
, int width
)
473 static char result
[BUF_MEDIUM
];
480 remain
= sizeof (result
);
481 length
= strlen (text
);
483 if (start
< (int)length
)
486 for (; pos
< length
&& width
> 0 && remain
> 1;
487 pos
++, width
--, actual
++, remain
--)
490 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
494 for (; width
> 0 && remain
> 1; actual
++, remain
--, width
--)
504 str_8bit_trunc (const char *text
, int width
)
506 static char result
[MC_MAXPATHLEN
];
513 remain
= sizeof (result
);
514 length
= strlen (text
);
516 if ((int)length
> width
)
518 for (; pos
+ 1 <= (gsize
)width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
520 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
529 pos
+= length
- width
+ 1;
531 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
533 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
538 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
540 actual
[0] = char_isprint (text
[pos
]) ? text
[pos
] : '.';
550 str_8bit_offset_to_pos (const char *text
, size_t length
)
557 str_8bit_column_to_pos (const char *text
, size_t pos
)
564 str_8bit_create_search_needle (const char *needle
, int case_sen
)
567 return (char *) needle
;
571 str_8bit_release_search_needle (char *needle
, int case_sen
)
578 str_8bit_strdown (const char *str
)
582 rets
= g_strdup (str
);
586 for (p
= rets
; *p
!= '\0'; p
++)
587 *p
= char_tolower (*p
);
594 str_8bit_search_first (const char *text
, const char *search
, int case_sen
)
601 fold_text
= (case_sen
) ? (char *) text
: str_8bit_strdown (text
);
602 fold_search
= (case_sen
) ? (char *) search
: str_8bit_strdown (search
);
604 match
= g_strstr_len (fold_text
, -1, fold_search
);
607 offsset
= match
- fold_text
;
608 match
= text
+ offsset
;
614 g_free (fold_search
);
621 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
);
634 offsset
= match
- fold_text
;
635 match
= text
+ offsset
;
641 g_free (fold_search
);
648 str_8bit_compare (const char *t1
, const char *t2
)
650 return strcmp (t1
, t2
);
654 str_8bit_ncompare (const char *t1
, const char *t2
)
656 return strncmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
660 str_8bit_casecmp (const char *s1
, const char *s2
)
664 #ifdef HAVE_STRCASECMP
665 g_return_val_if_fail (s1
!= NULL
, 0);
666 g_return_val_if_fail (s2
!= NULL
, 0);
668 return strcasecmp (s1
, s2
);
672 g_return_val_if_fail (s1
!= NULL
, 0);
673 g_return_val_if_fail (s2
!= NULL
, 0);
675 while (*s1
!= '\0' && *s2
!= '\0')
677 /* According to A. Cox, some platforms have islower's that
678 * don't work right on non-uppercase
680 c1
= isupper ((guchar
) *s1
) ? tolower ((guchar
) *s1
) : *s1
;
681 c2
= isupper ((guchar
) *s2
) ? tolower ((guchar
) *s2
) : *s2
;
688 return (((gint
)(guchar
) *s1
) - ((gint
)(guchar
) *s2
));
693 str_8bit_ncasecmp (const char *s1
, const char *s2
)
697 g_return_val_if_fail (s1
!= NULL
, 0);
698 g_return_val_if_fail (s2
!= NULL
, 0);
700 n
= min (strlen (s1
), strlen (s2
));
704 #ifdef HAVE_STRNCASECMP
705 return strncasecmp (s1
, s2
, n
);
709 while (n
!= 0 && *s1
!= '\0' && *s2
!= '\0')
712 /* According to A. Cox, some platforms have islower's that
713 * don't work right on non-uppercase
715 c1
= isupper ((guchar
) *s1
) ? tolower ((guchar
) *s1
) : *s1
;
716 c2
= isupper ((guchar
) *s2
) ? tolower ((guchar
) *s2
) : *s2
;
724 return (((gint
) (guchar
) *s1
) - ((gint
) (guchar
) *s2
));
731 str_8bit_prefix (const char *text
, const char *prefix
)
734 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
735 && text
[result
] == prefix
[result
]; result
++);
740 str_8bit_caseprefix (const char *text
, const char *prefix
)
743 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
744 && char_toupper (text
[result
]) == char_toupper (prefix
[result
]); result
++);
751 str_8bit_fix_string (char *text
)
757 str_8bit_create_key (const char *text
, int case_sen
)
759 return (case_sen
) ? (char *) text
: str_8bit_strdown (text
);
763 str_8bit_key_collate (const char *t1
, const char *t2
, int case_sen
)
766 return strcmp (t1
, t2
);
768 return strcoll (t1
, t2
);
772 str_8bit_release_key (char *key
, int case_sen
)
781 struct str_class result
;
783 result
.conv_gerror_message
= str_8bit_conv_gerror_message
;
784 result
.vfs_convert_to
= str_8bit_vfs_convert_to
;
785 result
.insert_replace_char
= str_8bit_insert_replace_char
;
786 result
.is_valid_string
= str_8bit_is_valid_string
;
787 result
.is_valid_char
= str_8bit_is_valid_char
;
788 result
.cnext_char
= str_8bit_cnext_char
;
789 result
.cprev_char
= str_8bit_cprev_char
;
790 result
.cnext_char_safe
= str_8bit_cnext_char
;
791 result
.cprev_char_safe
= str_8bit_cprev_char
;
792 result
.cnext_noncomb_char
= str_8bit_cnext_noncomb_char
;
793 result
.cprev_noncomb_char
= str_8bit_cprev_noncomb_char
;
794 result
.isspace
= str_8bit_isspace
;
795 result
.ispunct
= str_8bit_ispunct
;
796 result
.isalnum
= str_8bit_isalnum
;
797 result
.isdigit
= str_8bit_isdigit
;
798 result
.isprint
= str_8bit_isprint
;
799 result
.iscombiningmark
= str_8bit_iscombiningmark
;
800 result
.toupper
= str_8bit_toupper
;
801 result
.tolower
= str_8bit_tolower
;
802 result
.length
= str_8bit_length
;
803 result
.length2
= str_8bit_length2
;
804 result
.length_noncomb
= str_8bit_length
;
805 result
.fix_string
= str_8bit_fix_string
;
806 result
.term_form
= str_8bit_term_form
;
807 result
.fit_to_term
= str_8bit_fit_to_term
;
808 result
.term_trim
= str_8bit_term_trim
;
809 result
.term_width2
= str_8bit_term_width2
;
810 result
.term_width1
= str_8bit_term_width1
;
811 result
.term_char_width
= str_8bit_term_char_width
;
812 result
.msg_term_size
= str_8bit_msg_term_size
;
813 result
.term_substring
= str_8bit_term_substring
;
814 result
.trunc
= str_8bit_trunc
;
815 result
.offset_to_pos
= str_8bit_offset_to_pos
;
816 result
.column_to_pos
= str_8bit_column_to_pos
;
817 result
.create_search_needle
= str_8bit_create_search_needle
;
818 result
.release_search_needle
= str_8bit_release_search_needle
;
819 result
.search_first
= str_8bit_search_first
;
820 result
.search_last
= str_8bit_search_last
;
821 result
.compare
= str_8bit_compare
;
822 result
.ncompare
= str_8bit_ncompare
;
823 result
.casecmp
= str_8bit_casecmp
;
824 result
.ncasecmp
= str_8bit_ncasecmp
;
825 result
.prefix
= str_8bit_prefix
;
826 result
.caseprefix
= str_8bit_caseprefix
;
827 result
.create_key
= str_8bit_create_key
;
828 result
.create_key_for_filename
= str_8bit_create_key
;
829 result
.key_collate
= str_8bit_key_collate
;
830 result
.release_key
= str_8bit_release_key
;