summaryrefslogtreecommitdiffstats
path: root/lib/strutil/strutilascii.c
blob: 421bfdb35c0e3810e56908ad76f5602413d16505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
   ASCII strings utilities

   Copyright (C) 2007-2023
   Free Software Foundation, Inc.

   Written by:
   Rostislav Benes, 2007

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <ctype.h>
#include <stdlib.h>

#include "lib/global.h"
#include "lib/strutil.h"

/* using g_ascii function from glib
 * on terminal are showed only ascii characters (lower than 0x80)
 */

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

static const char replch = '?';

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

static void
str_ascii_insert_replace_char (GString * buffer)
{
    g_string_append_c (buffer, replch);
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_is_valid_string (const char *text)
{
    (void) text;
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_is_valid_char (const char *ch, size_t size)
{
    (void) ch;
    (void) size;
    return 1;
}

/* --------------------------------------------------------------------------------------------- */

static void
str_ascii_cnext_char (const char **text)
{
    (*text)++;
}

/* --------------------------------------------------------------------------------------------- */

static void
str_ascii_cprev_char (const char **text)
{
    (*text)--;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_cnext_noncomb_char (const char **text)
{
    if (*text[0] == '\0')
        return 0;

    (*text)++;
    return 1;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_cprev_noncomb_char (const char **text, const char *begin)
{
    if ((*text) == begin)
        return 0;

    (*text)--;
    return 1;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_isspace (const char *text)
{
    return g_ascii_isspace ((gchar) text[0]);
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_ispunct (const char *text)
{
    return g_ascii_ispunct ((gchar) text[0]);
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_isalnum (const char *text)
{
    return g_ascii_isalnum ((gchar) text[0]);
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_isdigit (const char *text)
{
    return g_ascii_isdigit ((gchar) text[0]);
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_isprint (const char *text)
{
    return g_ascii_isprint ((gchar) text[0]);
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_iscombiningmark (const char *text)
{
    (void) text;
    return FALSE;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_toupper (const char *text, char **out, size_t * remain)
{
    if (*remain <= 1)
        return FALSE;

    (*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
    (*out)++;
    (*remain)--;
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
str_ascii_tolower (const char *text, char **out, size_t * remain)
{
    if (*remain <= 1)
        return FALSE;

    (*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
    (*out)++;
    (*remain)--;
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_length (const char *text)
{
    return strlen (text);
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_length2 (const char *text, int size)
{
    return (size >= 0) ? MIN (strlen (text), (gsize) size) : strlen (text);
}

/* --------------------------------------------------------------------------------------------- */

static gchar *
str_ascii_conv_gerror_message (GError * mcerror, const char *def_msg)
{
    /* the same as str_utf8_conv_gerror_message() */
    if (mcerror != NULL)
        return g_strdup (mcerror->message);

    return g_strdup (def_msg != NULL ? def_msg : "");
}

/* --------------------------------------------------------------------------------------------- */

static estr_t
str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
{
    (void) coder;
    g_string_append_len (buffer, string, size);
    return ESTR_SUCCESS;
}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_term_form (const char *text)
{
    static char result[BUF_MEDIUM];
    char *actual;
    size_t remain;
    size_t length;
    size_t pos = 0;

    actual = result;
    remain = sizeof (result);
    length = strlen (text);

    /* go throw all characters and check, if they are ascii and printable */
    for (; pos < length && remain > 1; pos++, actual++, remain--)
    {
        actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
        actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
    }

    actual[0] = '\0';
    return result;
}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
{
    static char result[BUF_MEDIUM];
    char *actual;
    size_t remain;
    int ident = 0;
    size_t length;
    size_t pos = 0;

    length = strlen (text);
    actual = result;
    remain = sizeof (result);

    if ((int) length <= width)
    {
        switch (HIDE_FIT (just_mode))
        {
        case J_CENTER_LEFT:
        case J_CENTER:
            ident = (width - length) / 2;
            break;
        case J_RIGHT:
            ident = width - length;
            break;
        default:
            break;
        }

        /* add space before text */
        if ((int) remain <= ident)
            goto finally;
        memset (actual, ' ', ident);
        actual += ident;
        remain -= ident;

        /* copy all characters */
        for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

        /* add space after text */
        if (width - length - ident > 0)
        {
            if (remain <= width - length - ident)
                goto finally;
            memset (actual, ' ', width - length - ident);
            actual += width - length - ident;
        }
    }
    else if (IS_FIT (just_mode))
    {
        /* copy prefix of text, that is not wider than width / 2 */
        for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

        if (remain <= 1)
            goto finally;
        actual[0] = '~';
        actual++;
        remain--;

        pos += length - width + 1;

        /* copy suffix of text */
        for (; pos < length && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }
    }
    else
    {
        switch (HIDE_FIT (just_mode))
        {
        case J_CENTER:
            ident = (length - width) / 2;
            break;
        case J_RIGHT:
            ident = length - width;
            break;
        default:
            break;
        }

        /* copy substring text, substring start from ident and take width 
         * characters from text */
        pos += ident;
        for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

    }

  finally:
    if (actual >= result + sizeof (result))
        actual = result + sizeof (result) - 1;
    actual[0] = '\0';
    return result;
}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_term_trim (const char *text, int width)
{
    static char result[BUF_MEDIUM];
    size_t remain;
    char *actual;
    size_t length;

    length = strlen (text);
    actual = result;
    remain = sizeof (result);

    if (width > 0)
    {
        size_t pos;

        if (width >= (int) length)
        {
            /* copy all characters */
            for (pos = 0; pos < length && remain > 1; pos++, actual++, remain--)
            {
                actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
                actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
            }
        }
        else if (width <= 3)
        {
            memset (actual, '.', width);
            actual += width;
        }
        else
        {
            memset (actual, '.', 3);
            actual += 3;
            remain -= 3;

            /* copy suffix of text */
            for (pos = length - width + 3; pos < length && remain > 1; pos++, actual++, remain--)
            {
                actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
                actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
            }
        }
    }

    actual[0] = '\0';
    return result;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_term_width2 (const char *text, size_t length)
{
    return (length != (size_t) (-1)) ? MIN (strlen (text), length) : strlen (text);
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_term_width1 (const char *text)
{
    return str_ascii_term_width2 (text, (size_t) (-1));
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_term_char_width (const char *text)
{
    (void) text;
    return 1;
}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_term_substring (const char *text, int start, int width)
{
    static char result[BUF_MEDIUM];
    size_t remain;
    char *actual;
    size_t length;

    actual = result;
    remain = sizeof (result);
    length = strlen (text);

    if (start < (int) length)
    {
        size_t pos;

        /* copy at most width characters from text from start */
        for (pos = start; pos < length && width > 0 && remain > 1;
             pos++, width--, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }
    }

    /* if text is shorter then width, add space to the end */
    for (; width > 0 && remain > 1; actual++, remain--, width--)
        actual[0] = ' ';

    actual[0] = '\0';
    return result;
}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_trunc (const char *text, int width)
{
    static char result[MC_MAXPATHLEN];
    int remain;
    char *actual;
    size_t pos = 0;
    size_t length;

    actual = result;
    remain = sizeof (result);
    length = strlen (text);

    if ((int) length > width)
    {
        /* copy prefix of text */
        for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

        if (remain <= 1)
            goto finally;
        actual[0] = '~';
        actual++;
        remain--;

        pos += length - width + 1;

        /* copy suffix of text */
        for (; pos < length && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }
    }
    else
    {
        /* copy all characters */
        for (; pos < length && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }
    }

  finally:
    actual[0] = '\0';
    return result;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_offset_to_pos (const char *text, size_t length)
{
    (void) text;
    return (int) length;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_column_to_pos (const char *text, size_t pos)
{
    (void) text;
    return (int) pos;
}

/* --------------------------------------------------------------------------------------------- */

static char *
str_ascii_create_search_needle (const char *needle, gboolean case_sen)
{
    (void) case_sen;
    return (char *) needle;
}

/* --------------------------------------------------------------------------------------------- */

static void
str_ascii_release_search_needle (char *needle, gboolean case_sen)
{
    (void) case_sen;
    (void) needle;

}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_search_first (const char *text, const char *search, gboolean case_sen)
{
    char *fold_text;
    char *fold_search;
    const char *match;

    fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
    fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);

    match = g_strstr_len (fold_text, -1, fold_search);
    if (match != NULL)
    {
        size_t offset;

        offset = match - fold_text;
        match = text + offset;
    }

    if (!case_sen)
    {
        g_free (fold_text);
        g_free (fold_search);
    }

    return match;
}

/* --------------------------------------------------------------------------------------------- */

static const char *
str_ascii_search_last (const char *text, const char *search, gboolean case_sen)
{
    char *fold_text;
    char *fold_search;
    const char *match;

    fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
    fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);

    match = g_strrstr_len (fold_text, -1, fold_search);
    if (match != NULL)
    {
        size_t offset;

        offset = match - fold_text;
        match = text + offset;
    }

    if (!case_sen)
    {
        g_free (fold_text);
        g_free (fold_search);
    }

    return match;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_compare (const char *t1, const char *t2)
{
    return strcmp (t1, t2);
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_ncompare (const char *t1, const char *t2)
{
    return strncmp (t1, t2, MIN (strlen (t1), strlen (t2)));
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_casecmp (const char *t1, const char *t2)
{
    return g_ascii_strcasecmp (t1, t2);
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_ncasecmp (const char *t1, const char *t2)
{
    return g_ascii_strncasecmp (t1, t2, MIN (strlen (t1), strlen (t2)));
}

/* --------------------------------------------------------------------------------------------- */

static void
str_ascii_fix_string (char *text)
{
    for (; text[0] != '\0'; text++)
        text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
}

/* --------------------------------------------------------------------------------------------- */

static char *
str_ascii_create_key (const char *text, gboolean case_sen)
{
    (void) case_sen;
    return (char *) text;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_key_collate (const char *t1, const char *t2, gboolean case_sen)
{
    return case_sen ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
}

/* --------------------------------------------------------------------------------------------- */

static void
str_ascii_release_key (char *key, gboolean case_sen)
{
    (void) key;
    (void) case_sen;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_prefix (const char *text, const char *prefix)
{
    int result;

    for (result = 0; text[result] != '\0' && prefix[result] != '\0'
         && text[result] == prefix[result]; result++);

    return result;
}

/* --------------------------------------------------------------------------------------------- */

static int
str_ascii_caseprefix (const char *text, const char *prefix)
{
    int result;

    for (result = 0; text[result] != '\0' && prefix[result] != '\0'
         && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);

    return result;
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

struct str_class
str_ascii_init (void)
{
    struct str_class result;

    result.conv_gerror_message = str_ascii_conv_gerror_message;
    result.vfs_convert_to = str_ascii_vfs_convert_to;
    result.insert_replace_char = str_ascii_insert_replace_char;
    result.is_valid_string = str_ascii_is_valid_string;
    result.is_valid_char = str_ascii_is_valid_char;
    result.cnext_char = str_ascii_cnext_char;
    result.cprev_char = str_ascii_cprev_char;
    result.cnext_char_safe = str_ascii_cnext_char;
    result.cprev_char_safe = str_ascii_cprev_char;
    result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
    result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
    result.char_isspace = str_ascii_isspace;
    result.char_ispunct = str_ascii_ispunct;
    result.char_isalnum = str_ascii_isalnum;
    result.char_isdigit = str_ascii_isdigit;
    result.char_isprint = str_ascii_isprint;
    result.char_iscombiningmark = str_ascii_iscombiningmark;
    result.char_toupper = str_ascii_toupper;
    result.char_tolower = str_ascii_tolower;
    result.length = str_ascii_length;
    result.length2 = str_ascii_length2;
    result.length_noncomb = str_ascii_length;
    result.fix_string = str_ascii_fix_string;
    result.term_form = str_ascii_term_form;
    result.fit_to_term = str_ascii_fit_to_term;
    result.term_trim = str_ascii_term_trim;
    result.term_width2 = str_ascii_term_width2;
    result.term_width1 = str_ascii_term_width1;
    result.term_char_width = str_ascii_term_char_width;
    result.term_substring = str_ascii_term_substring;
    result.trunc = str_ascii_trunc;
    result.offset_to_pos = str_ascii_offset_to_pos;
    result.column_to_pos = str_ascii_column_to_pos;
    result.create_search_needle = str_ascii_create_search_needle;
    result.release_search_needle = str_ascii_release_search_needle;
    result.search_first = str_ascii_search_first;
    result.search_last = str_ascii_search_last;
    result.compare = str_ascii_compare;
    result.ncompare = str_ascii_ncompare;
    result.casecmp = str_ascii_casecmp;
    result.ncasecmp = str_ascii_ncasecmp;
    result.prefix = str_ascii_prefix;
    result.caseprefix = str_ascii_caseprefix;
    result.create_key = str_ascii_create_key;
    result.create_key_for_filename = str_ascii_create_key;
    result.key_collate = str_ascii_key_collate;
    result.release_key = str_ascii_release_key;

    return result;
}

/* --------------------------------------------------------------------------------------------- */