| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
str_process.c - description |
/* |
| 3 |
------------------- |
* str_process |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - common string processing features with UTF-8 support |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program 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. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "common.h" |
#include "common.h" |
| 10 |
#include "log.h" |
#include "log.h" |
| 11 |
#include "str_process.h" |
#include "str_process.h" |
| 12 |
|
#include <ctype.h> |
| 13 |
#include <stdio.h> |
#include <stdio.h> |
| 14 |
#include <string.h> |
#include <string.h> |
| 15 |
|
|
| 30 |
|
|
| 31 |
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence |
| 32 |
{ |
{ |
| 33 |
i += 2; |
for (i = i + 2; isdigit(str[i]) || str[i] == ';' || str[i] == '?'; i++) |
| 34 |
while (str[i] != '\0' && str[i] != 'm') |
; |
| 35 |
|
|
| 36 |
|
if (str[i] == 'm') // valid |
| 37 |
{ |
{ |
| 38 |
i++; |
// skip |
| 39 |
|
} |
| 40 |
|
else if (isalpha(str[i])) |
| 41 |
|
{ |
| 42 |
|
// unsupported ANSI CSI command |
| 43 |
|
} |
| 44 |
|
else |
| 45 |
|
{ |
| 46 |
|
i--; |
| 47 |
} |
} |
| 48 |
|
|
| 49 |
continue; |
continue; |
| 50 |
} |
} |
| 51 |
|
|
| 52 |
// Process UTF-8 Chinese characters |
// Process UTF-8 Chinese characters |
| 53 |
if (c & 0b10000000) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 54 |
{ |
{ |
| 55 |
c = (c & 0b01110000) << 1; |
c = (c & 0x70) << 1; |
| 56 |
while (c & 0b10000000) |
while (c & 0x80) |
| 57 |
{ |
{ |
| 58 |
i++; |
i++; |
| 59 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 60 |
} |
} |
| 61 |
|
|
| 62 |
ret += 2; |
ret += 2; |
| 96 |
continue; |
continue; |
| 97 |
} |
} |
| 98 |
|
|
| 99 |
if (c & 0b10000000) // head of multi-byte character |
if (c & 0x80) // head of multi-byte character |
| 100 |
{ |
{ |
| 101 |
if (*p_display_len + 2 > max_display_len) |
if (*p_display_len + 2 > max_display_len) |
| 102 |
{ |
{ |
| 103 |
break; |
break; |
| 104 |
} |
} |
| 105 |
|
|
| 106 |
c = (c & 0b01110000) << 1; |
c = (c & 0x70) << 1; |
| 107 |
while (c & 0b10000000) |
while (c & 0x80) |
| 108 |
{ |
{ |
| 109 |
i++; |
i++; |
| 110 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 111 |
} |
} |
| 112 |
|
|
| 113 |
(*p_display_len) += 2; |
(*p_display_len) += 2; |