1 /* ASCII 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 /* using g_ascii function from glib
34 * on terminal are showed only ascii characters (lower then 0x80)
37 static const char replch
= '?';
40 str_ascii_insert_replace_char (GString
* buffer
)
42 g_string_append_c (buffer
, replch
);
46 str_ascii_is_valid_string (const char *text
)
53 str_ascii_is_valid_char (const char *ch
, size_t size
)
61 str_ascii_cnext_char (const char **text
)
67 str_ascii_cprev_char (const char **text
)
73 str_ascii_cnext_noncomb_char (const char **text
)
85 str_ascii_cprev_noncomb_char (const char **text
, const char *begin
)
97 str_ascii_isspace (const char *text
)
99 return g_ascii_isspace ((gchar
) text
[0]);
103 str_ascii_ispunct (const char *text
)
105 return g_ascii_ispunct ((gchar
) text
[0]);
109 str_ascii_isalnum (const char *text
)
111 return g_ascii_isalnum ((gchar
) text
[0]);
115 str_ascii_isdigit (const char *text
)
117 return g_ascii_isdigit ((gchar
) text
[0]);
121 str_ascii_isprint (const char *text
)
123 return g_ascii_isprint ((gchar
) text
[0]);
127 str_ascii_iscombiningmark (const char *text
)
134 str_ascii_toupper (const char *text
, char **out
, size_t * remain
)
138 (*out
)[0] = (char) g_ascii_toupper ((gchar
) text
[0]);
145 str_ascii_tolower (const char *text
, char **out
, size_t * remain
)
149 (*out
)[0] = (char) g_ascii_tolower ((gchar
) text
[0]);
156 str_ascii_length (const char *text
)
158 return strlen (text
);
162 str_ascii_length2 (const char *text
, int size
)
164 return (size
>= 0) ? min (strlen (text
), (gsize
) size
) : strlen (text
);
168 str_ascii_conv_gerror_message (GError
* error
, const char *def_msg
)
170 /* the same as str_utf8_conv_gerror_message() */
171 if ((error
!= NULL
) && (error
->message
!= NULL
))
172 return g_strdup (error
->message
);
174 return g_strdup (def_msg
!= NULL
? def_msg
: "");
178 str_ascii_vfs_convert_to (GIConv coder
, const char *string
, int size
, GString
* buffer
)
181 g_string_append_len (buffer
, string
, size
);
187 str_ascii_term_form (const char *text
)
189 static char result
[BUF_MEDIUM
];
196 remain
= sizeof (result
);
197 length
= strlen (text
);
199 /* go throw all characters and check, if they are ascii and printable */
200 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
202 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
203 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
211 str_ascii_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
213 static char result
[BUF_MEDIUM
];
220 length
= strlen (text
);
222 remain
= sizeof (result
);
224 if ((int) length
<= width
)
227 switch (HIDE_FIT (just_mode
))
231 ident
= (width
- length
) / 2;
234 ident
= width
- length
;
238 /* add space before text */
239 if ((int) remain
<= ident
)
241 memset (actual
, ' ', ident
);
245 /* copy all characters */
246 for (; pos
< (gsize
) length
&& remain
> 1; pos
++, actual
++, remain
--)
248 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
249 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
252 /* add space after text */
253 if (width
- length
- ident
> 0)
255 if (remain
<= width
- length
- ident
)
257 memset (actual
, ' ', width
- length
- ident
);
258 actual
+= width
- length
- ident
;
259 remain
-= width
- length
- ident
;
264 if (IS_FIT (just_mode
))
266 /* copy prefix of text, that is not wider than width / 2 */
267 for (; pos
+ 1 <= (gsize
) width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
269 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
270 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
279 pos
+= length
- width
+ 1;
281 /* copy suffix of text */
282 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
284 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
285 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
291 switch (HIDE_FIT (just_mode
))
294 ident
= (length
- width
) / 2;
297 ident
= length
- width
;
301 /* copy substring text, substring start from ident and take width
302 * characters from text */
304 for (; pos
< (gsize
) (ident
+ width
) && remain
> 1; pos
++, actual
++, remain
--)
306 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
307 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
318 str_ascii_term_trim (const char *text
, int width
)
320 static char result
[BUF_MEDIUM
];
326 length
= strlen (text
);
328 remain
= sizeof (result
);
330 if (width
< (int) length
)
334 memset (actual
, '.', width
);
340 memset (actual
, '.', 3);
344 pos
+= length
- width
+ 3;
346 /* copy suffix of text */
347 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
349 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
350 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
356 /* copy all characters */
357 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
359 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
360 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
369 str_ascii_term_width2 (const char *text
, size_t length
)
371 return (length
!= (size_t) (-1)) ? min (strlen (text
), length
) : strlen (text
);
375 str_ascii_term_width1 (const char *text
)
377 return str_ascii_term_width2 (text
, (size_t) (-1));
381 str_ascii_term_char_width (const char *text
)
388 str_ascii_term_substring (const char *text
, int start
, int width
)
390 static char result
[BUF_MEDIUM
];
397 remain
= sizeof (result
);
398 length
= strlen (text
);
400 if (start
< (int) length
)
403 /* copy at most width characters from text from start */
404 for (; pos
< length
&& width
> 0 && remain
> 1; pos
++, width
--, actual
++, remain
--)
407 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
408 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
412 /* if text is shorter then width, add space to the end */
413 for (; width
> 0 && remain
> 1; actual
++, remain
--, width
--)
423 str_ascii_trunc (const char *text
, int width
)
425 static char result
[MC_MAXPATHLEN
];
432 remain
= sizeof (result
);
433 length
= strlen (text
);
435 if ((int) length
> width
)
437 /* copy prefix of text */
438 for (; pos
+ 1 <= (gsize
) width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
440 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
441 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
450 pos
+= length
- width
+ 1;
452 /* copy suffix of text */
453 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
455 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
456 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
461 /* copy all characters */
462 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
464 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
465 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
475 str_ascii_offset_to_pos (const char *text
, size_t length
)
482 str_ascii_column_to_pos (const char *text
, size_t pos
)
489 str_ascii_create_search_needle (const char *needle
, int case_sen
)
492 return (char *) needle
;
496 str_ascii_release_search_needle (char *needle
, int case_sen
)
504 str_ascii_search_first (const char *text
, const char *search
, int case_sen
)
511 fold_text
= (case_sen
) ? (char *) text
: g_ascii_strdown (text
, -1);
512 fold_search
= (case_sen
) ? (char *) search
: g_ascii_strdown (search
, -1);
514 match
= g_strstr_len (fold_text
, -1, fold_search
);
517 offset
= match
- fold_text
;
518 match
= text
+ offset
;
524 g_free (fold_search
);
531 str_ascii_search_last (const char *text
, const char *search
, int case_sen
)
538 fold_text
= (case_sen
) ? (char *) text
: g_ascii_strdown (text
, -1);
539 fold_search
= (case_sen
) ? (char *) search
: g_ascii_strdown (search
, -1);
541 match
= g_strrstr_len (fold_text
, -1, fold_search
);
544 offset
= match
- fold_text
;
545 match
= text
+ offset
;
551 g_free (fold_search
);
558 str_ascii_compare (const char *t1
, const char *t2
)
560 return strcmp (t1
, t2
);
564 str_ascii_ncompare (const char *t1
, const char *t2
)
566 return strncmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
570 str_ascii_casecmp (const char *t1
, const char *t2
)
572 return g_ascii_strcasecmp (t1
, t2
);
576 str_ascii_ncasecmp (const char *t1
, const char *t2
)
578 return g_ascii_strncasecmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
582 str_ascii_fix_string (char *text
)
584 for (; text
[0] != '\0'; text
++)
586 text
[0] = ((unsigned char) text
[0] < 128) ? text
[0] : '?';
591 str_ascii_create_key (const char *text
, int case_sen
)
594 return (char *) text
;
598 str_ascii_key_collate (const char *t1
, const char *t2
, int case_sen
)
600 return (case_sen
) ? strcmp (t1
, t2
) : g_ascii_strcasecmp (t1
, t2
);
604 str_ascii_release_key (char *key
, int case_sen
)
611 str_ascii_prefix (const char *text
, const char *prefix
)
614 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
615 && text
[result
] == prefix
[result
]; result
++);
620 str_ascii_caseprefix (const char *text
, const char *prefix
)
623 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
624 && g_ascii_toupper (text
[result
]) == g_ascii_toupper (prefix
[result
]); result
++);
630 str_ascii_init (void)
632 struct str_class result
;
634 result
.conv_gerror_message
= str_ascii_conv_gerror_message
;
635 result
.vfs_convert_to
= str_ascii_vfs_convert_to
;
636 result
.insert_replace_char
= str_ascii_insert_replace_char
;
637 result
.is_valid_string
= str_ascii_is_valid_string
;
638 result
.is_valid_char
= str_ascii_is_valid_char
;
639 result
.cnext_char
= str_ascii_cnext_char
;
640 result
.cprev_char
= str_ascii_cprev_char
;
641 result
.cnext_char_safe
= str_ascii_cnext_char
;
642 result
.cprev_char_safe
= str_ascii_cprev_char
;
643 result
.cnext_noncomb_char
= str_ascii_cnext_noncomb_char
;
644 result
.cprev_noncomb_char
= str_ascii_cprev_noncomb_char
;
645 result
.isspace
= str_ascii_isspace
;
646 result
.ispunct
= str_ascii_ispunct
;
647 result
.isalnum
= str_ascii_isalnum
;
648 result
.isdigit
= str_ascii_isdigit
;
649 result
.isprint
= str_ascii_isprint
;
650 result
.iscombiningmark
= str_ascii_iscombiningmark
;
651 result
.toupper
= str_ascii_toupper
;
652 result
.tolower
= str_ascii_tolower
;
653 result
.length
= str_ascii_length
;
654 result
.length2
= str_ascii_length2
;
655 result
.length_noncomb
= str_ascii_length
;
656 result
.fix_string
= str_ascii_fix_string
;
657 result
.term_form
= str_ascii_term_form
;
658 result
.fit_to_term
= str_ascii_fit_to_term
;
659 result
.term_trim
= str_ascii_term_trim
;
660 result
.term_width2
= str_ascii_term_width2
;
661 result
.term_width1
= str_ascii_term_width1
;
662 result
.term_char_width
= str_ascii_term_char_width
;
663 result
.term_substring
= str_ascii_term_substring
;
664 result
.trunc
= str_ascii_trunc
;
665 result
.offset_to_pos
= str_ascii_offset_to_pos
;
666 result
.column_to_pos
= str_ascii_column_to_pos
;
667 result
.create_search_needle
= str_ascii_create_search_needle
;
668 result
.release_search_needle
= str_ascii_release_search_needle
;
669 result
.search_first
= str_ascii_search_first
;
670 result
.search_last
= str_ascii_search_last
;
671 result
.compare
= str_ascii_compare
;
672 result
.ncompare
= str_ascii_ncompare
;
673 result
.casecmp
= str_ascii_casecmp
;
674 result
.ncasecmp
= str_ascii_ncasecmp
;
675 result
.prefix
= str_ascii_prefix
;
676 result
.caseprefix
= str_ascii_caseprefix
;
677 result
.create_key
= str_ascii_create_key
;
678 result
.create_key_for_filename
= str_ascii_create_key
;
679 result
.key_collate
= str_ascii_key_collate
;
680 result
.release_key
= str_ascii_release_key
;