2 ASCII strings utilities
4 Copyright (C) 2007, 2011
5 The 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 /* using g_ascii function from glib
35 * on terminal are showed only ascii characters (lower then 0x80)
38 static const char replch
= '?';
41 str_ascii_insert_replace_char (GString
* buffer
)
43 g_string_append_c (buffer
, replch
);
47 str_ascii_is_valid_string (const char *text
)
54 str_ascii_is_valid_char (const char *ch
, size_t size
)
62 str_ascii_cnext_char (const char **text
)
68 str_ascii_cprev_char (const char **text
)
74 str_ascii_cnext_noncomb_char (const char **text
)
86 str_ascii_cprev_noncomb_char (const char **text
, const char *begin
)
98 str_ascii_isspace (const char *text
)
100 return g_ascii_isspace ((gchar
) text
[0]);
104 str_ascii_ispunct (const char *text
)
106 return g_ascii_ispunct ((gchar
) text
[0]);
110 str_ascii_isalnum (const char *text
)
112 return g_ascii_isalnum ((gchar
) text
[0]);
116 str_ascii_isdigit (const char *text
)
118 return g_ascii_isdigit ((gchar
) text
[0]);
122 str_ascii_isprint (const char *text
)
124 return g_ascii_isprint ((gchar
) text
[0]);
128 str_ascii_iscombiningmark (const char *text
)
135 str_ascii_toupper (const char *text
, char **out
, size_t * remain
)
139 (*out
)[0] = (char) g_ascii_toupper ((gchar
) text
[0]);
146 str_ascii_tolower (const char *text
, char **out
, size_t * remain
)
150 (*out
)[0] = (char) g_ascii_tolower ((gchar
) text
[0]);
157 str_ascii_length (const char *text
)
159 return strlen (text
);
163 str_ascii_length2 (const char *text
, int size
)
165 return (size
>= 0) ? min (strlen (text
), (gsize
) size
) : strlen (text
);
169 str_ascii_conv_gerror_message (GError
* error
, const char *def_msg
)
171 /* the same as str_utf8_conv_gerror_message() */
172 if ((error
!= NULL
) && (error
->message
!= NULL
))
173 return g_strdup (error
->message
);
175 return g_strdup (def_msg
!= NULL
? def_msg
: "");
179 str_ascii_vfs_convert_to (GIConv coder
, const char *string
, int size
, GString
* buffer
)
182 g_string_append_len (buffer
, string
, size
);
188 str_ascii_term_form (const char *text
)
190 static char result
[BUF_MEDIUM
];
197 remain
= sizeof (result
);
198 length
= strlen (text
);
200 /* go throw all characters and check, if they are ascii and printable */
201 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
203 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
204 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
212 str_ascii_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
214 static char result
[BUF_MEDIUM
];
221 length
= strlen (text
);
223 remain
= sizeof (result
);
225 if ((int) length
<= width
)
228 switch (HIDE_FIT (just_mode
))
232 ident
= (width
- length
) / 2;
235 ident
= width
- length
;
239 /* add space before text */
240 if ((int) remain
<= ident
)
242 memset (actual
, ' ', ident
);
246 /* copy all characters */
247 for (; pos
< (gsize
) length
&& remain
> 1; pos
++, actual
++, remain
--)
249 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
250 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
253 /* add space after text */
254 if (width
- length
- ident
> 0)
256 if (remain
<= width
- length
- ident
)
258 memset (actual
, ' ', width
- length
- ident
);
259 actual
+= 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
);
333 if (width
< (int) length
)
337 memset (actual
, '.', width
);
342 memset (actual
, '.', 3);
346 pos
+= length
- width
+ 3;
348 /* copy suffix of text */
349 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
351 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
352 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
358 /* copy all characters */
359 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
361 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
362 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
372 str_ascii_term_width2 (const char *text
, size_t length
)
374 return (length
!= (size_t) (-1)) ? min (strlen (text
), length
) : strlen (text
);
378 str_ascii_term_width1 (const char *text
)
380 return str_ascii_term_width2 (text
, (size_t) (-1));
384 str_ascii_term_char_width (const char *text
)
391 str_ascii_term_substring (const char *text
, int start
, int width
)
393 static char result
[BUF_MEDIUM
];
400 remain
= sizeof (result
);
401 length
= strlen (text
);
403 if (start
< (int) length
)
406 /* copy at most width characters from text from start */
407 for (; pos
< length
&& width
> 0 && remain
> 1; pos
++, width
--, actual
++, remain
--)
410 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
411 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
415 /* if text is shorter then width, add space to the end */
416 for (; width
> 0 && remain
> 1; actual
++, remain
--, width
--)
426 str_ascii_trunc (const char *text
, int width
)
428 static char result
[MC_MAXPATHLEN
];
435 remain
= sizeof (result
);
436 length
= strlen (text
);
438 if ((int) length
> width
)
440 /* copy prefix of text */
441 for (; pos
+ 1 <= (gsize
) width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
443 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
444 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
453 pos
+= length
- width
+ 1;
455 /* copy suffix of text */
456 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
458 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
459 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
464 /* copy all characters */
465 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
467 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
468 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
478 str_ascii_offset_to_pos (const char *text
, size_t length
)
485 str_ascii_column_to_pos (const char *text
, size_t pos
)
492 str_ascii_create_search_needle (const char *needle
, int case_sen
)
495 return (char *) needle
;
499 str_ascii_release_search_needle (char *needle
, int case_sen
)
507 str_ascii_search_first (const char *text
, const char *search
, int case_sen
)
514 fold_text
= (case_sen
) ? (char *) text
: g_ascii_strdown (text
, -1);
515 fold_search
= (case_sen
) ? (char *) search
: g_ascii_strdown (search
, -1);
517 match
= g_strstr_len (fold_text
, -1, fold_search
);
520 offset
= match
- fold_text
;
521 match
= text
+ offset
;
527 g_free (fold_search
);
534 str_ascii_search_last (const char *text
, const char *search
, int case_sen
)
541 fold_text
= (case_sen
) ? (char *) text
: g_ascii_strdown (text
, -1);
542 fold_search
= (case_sen
) ? (char *) search
: g_ascii_strdown (search
, -1);
544 match
= g_strrstr_len (fold_text
, -1, fold_search
);
547 offset
= match
- fold_text
;
548 match
= text
+ offset
;
554 g_free (fold_search
);
561 str_ascii_compare (const char *t1
, const char *t2
)
563 return strcmp (t1
, t2
);
567 str_ascii_ncompare (const char *t1
, const char *t2
)
569 return strncmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
573 str_ascii_casecmp (const char *t1
, const char *t2
)
575 return g_ascii_strcasecmp (t1
, t2
);
579 str_ascii_ncasecmp (const char *t1
, const char *t2
)
581 return g_ascii_strncasecmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
585 str_ascii_fix_string (char *text
)
587 for (; text
[0] != '\0'; text
++)
589 text
[0] = ((unsigned char) text
[0] < 128) ? text
[0] : '?';
594 str_ascii_create_key (const char *text
, int case_sen
)
597 return (char *) text
;
601 str_ascii_key_collate (const char *t1
, const char *t2
, int case_sen
)
603 return (case_sen
) ? strcmp (t1
, t2
) : g_ascii_strcasecmp (t1
, t2
);
607 str_ascii_release_key (char *key
, int case_sen
)
614 str_ascii_prefix (const char *text
, const char *prefix
)
617 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
618 && text
[result
] == prefix
[result
]; result
++);
623 str_ascii_caseprefix (const char *text
, const char *prefix
)
626 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
627 && g_ascii_toupper (text
[result
]) == g_ascii_toupper (prefix
[result
]); result
++);
633 str_ascii_init (void)
635 struct str_class result
;
637 result
.conv_gerror_message
= str_ascii_conv_gerror_message
;
638 result
.vfs_convert_to
= str_ascii_vfs_convert_to
;
639 result
.insert_replace_char
= str_ascii_insert_replace_char
;
640 result
.is_valid_string
= str_ascii_is_valid_string
;
641 result
.is_valid_char
= str_ascii_is_valid_char
;
642 result
.cnext_char
= str_ascii_cnext_char
;
643 result
.cprev_char
= str_ascii_cprev_char
;
644 result
.cnext_char_safe
= str_ascii_cnext_char
;
645 result
.cprev_char_safe
= str_ascii_cprev_char
;
646 result
.cnext_noncomb_char
= str_ascii_cnext_noncomb_char
;
647 result
.cprev_noncomb_char
= str_ascii_cprev_noncomb_char
;
648 result
.char_isspace
= str_ascii_isspace
;
649 result
.char_ispunct
= str_ascii_ispunct
;
650 result
.char_isalnum
= str_ascii_isalnum
;
651 result
.char_isdigit
= str_ascii_isdigit
;
652 result
.char_isprint
= str_ascii_isprint
;
653 result
.char_iscombiningmark
= str_ascii_iscombiningmark
;
654 result
.char_toupper
= str_ascii_toupper
;
655 result
.char_tolower
= str_ascii_tolower
;
656 result
.length
= str_ascii_length
;
657 result
.length2
= str_ascii_length2
;
658 result
.length_noncomb
= str_ascii_length
;
659 result
.fix_string
= str_ascii_fix_string
;
660 result
.term_form
= str_ascii_term_form
;
661 result
.fit_to_term
= str_ascii_fit_to_term
;
662 result
.term_trim
= str_ascii_term_trim
;
663 result
.term_width2
= str_ascii_term_width2
;
664 result
.term_width1
= str_ascii_term_width1
;
665 result
.term_char_width
= str_ascii_term_char_width
;
666 result
.term_substring
= str_ascii_term_substring
;
667 result
.trunc
= str_ascii_trunc
;
668 result
.offset_to_pos
= str_ascii_offset_to_pos
;
669 result
.column_to_pos
= str_ascii_column_to_pos
;
670 result
.create_search_needle
= str_ascii_create_search_needle
;
671 result
.release_search_needle
= str_ascii_release_search_needle
;
672 result
.search_first
= str_ascii_search_first
;
673 result
.search_last
= str_ascii_search_last
;
674 result
.compare
= str_ascii_compare
;
675 result
.ncompare
= str_ascii_ncompare
;
676 result
.casecmp
= str_ascii_casecmp
;
677 result
.ncasecmp
= str_ascii_ncasecmp
;
678 result
.prefix
= str_ascii_prefix
;
679 result
.caseprefix
= str_ascii_caseprefix
;
680 result
.create_key
= str_ascii_create_key
;
681 result
.create_key_for_filename
= str_ascii_create_key
;
682 result
.key_collate
= str_ascii_key_collate
;
683 result
.release_key
= str_ascii_release_key
;