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_search_first (const char *text
, const char *search
, int case_sen
)
585 fold_text
= (case_sen
) ? (char *) text
: g_strdown (g_strdup (text
));
586 fold_search
= (case_sen
) ? (char *) search
: g_strdown (g_strdup (search
));
588 match
= g_strstr_len (fold_text
, -1, fold_search
);
591 offsset
= match
- fold_text
;
592 match
= text
+ offsset
;
598 g_free (fold_search
);
605 str_8bit_search_last (const char *text
, const char *search
, int case_sen
)
612 fold_text
= (case_sen
) ? (char *) text
: g_strdown (g_strdup (text
));
613 fold_search
= (case_sen
) ? (char *) search
: g_strdown (g_strdup (search
));
615 match
= g_strrstr_len (fold_text
, -1, fold_search
);
618 offsset
= match
- fold_text
;
619 match
= text
+ offsset
;
625 g_free (fold_search
);
632 str_8bit_compare (const char *t1
, const char *t2
)
634 return strcmp (t1
, t2
);
638 str_8bit_ncompare (const char *t1
, const char *t2
)
640 return strncmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
644 str_8bit_casecmp (const char *t1
, const char *t2
)
646 return g_strcasecmp (t1
, t2
);
650 str_8bit_ncasecmp (const char *t1
, const char *t2
)
652 return g_strncasecmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
656 str_8bit_prefix (const char *text
, const char *prefix
)
659 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
660 && text
[result
] == prefix
[result
]; result
++);
665 str_8bit_caseprefix (const char *text
, const char *prefix
)
668 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
669 && char_toupper (text
[result
]) == char_toupper (prefix
[result
]); result
++);
676 str_8bit_fix_string (char *text
)
682 str_8bit_create_key (const char *text
, int case_sen
)
684 return (case_sen
) ? (char *) text
: g_strdown (g_strdup (text
));
688 str_8bit_key_collate (const char *t1
, const char *t2
, int case_sen
)
691 return strcmp (t1
, t2
);
693 return strcoll (t1
, t2
);
697 str_8bit_release_key (char *key
, int case_sen
)
706 struct str_class result
;
708 result
.conv_gerror_message
= str_8bit_conv_gerror_message
;
709 result
.vfs_convert_to
= str_8bit_vfs_convert_to
;
710 result
.insert_replace_char
= str_8bit_insert_replace_char
;
711 result
.is_valid_string
= str_8bit_is_valid_string
;
712 result
.is_valid_char
= str_8bit_is_valid_char
;
713 result
.cnext_char
= str_8bit_cnext_char
;
714 result
.cprev_char
= str_8bit_cprev_char
;
715 result
.cnext_char_safe
= str_8bit_cnext_char
;
716 result
.cprev_char_safe
= str_8bit_cprev_char
;
717 result
.cnext_noncomb_char
= str_8bit_cnext_noncomb_char
;
718 result
.cprev_noncomb_char
= str_8bit_cprev_noncomb_char
;
719 result
.isspace
= str_8bit_isspace
;
720 result
.ispunct
= str_8bit_ispunct
;
721 result
.isalnum
= str_8bit_isalnum
;
722 result
.isdigit
= str_8bit_isdigit
;
723 result
.isprint
= str_8bit_isprint
;
724 result
.iscombiningmark
= str_8bit_iscombiningmark
;
725 result
.toupper
= str_8bit_toupper
;
726 result
.tolower
= str_8bit_tolower
;
727 result
.length
= str_8bit_length
;
728 result
.length2
= str_8bit_length2
;
729 result
.length_noncomb
= str_8bit_length
;
730 result
.fix_string
= str_8bit_fix_string
;
731 result
.term_form
= str_8bit_term_form
;
732 result
.fit_to_term
= str_8bit_fit_to_term
;
733 result
.term_trim
= str_8bit_term_trim
;
734 result
.term_width2
= str_8bit_term_width2
;
735 result
.term_width1
= str_8bit_term_width1
;
736 result
.term_char_width
= str_8bit_term_char_width
;
737 result
.msg_term_size
= str_8bit_msg_term_size
;
738 result
.term_substring
= str_8bit_term_substring
;
739 result
.trunc
= str_8bit_trunc
;
740 result
.offset_to_pos
= str_8bit_offset_to_pos
;
741 result
.column_to_pos
= str_8bit_column_to_pos
;
742 result
.create_search_needle
= str_8bit_create_search_needle
;
743 result
.release_search_needle
= str_8bit_release_search_needle
;
744 result
.search_first
= str_8bit_search_first
;
745 result
.search_last
= str_8bit_search_last
;
746 result
.compare
= str_8bit_compare
;
747 result
.ncompare
= str_8bit_ncompare
;
748 result
.casecmp
= str_8bit_casecmp
;
749 result
.ncasecmp
= str_8bit_ncasecmp
;
750 result
.prefix
= str_8bit_prefix
;
751 result
.caseprefix
= str_8bit_caseprefix
;
752 result
.create_key
= str_8bit_create_key
;
753 result
.create_key_for_filename
= str_8bit_create_key
;
754 result
.key_collate
= str_8bit_key_collate
;
755 result
.release_key
= str_8bit_release_key
;