Convert unicode-utils.[ch] to 4-space indentation

This commit is contained in:
João Valverde 2023-02-06 13:49:05 +00:00
parent 7a33d04056
commit 2855c8ec46
3 changed files with 101 additions and 108 deletions

View File

@ -103,9 +103,6 @@ indent_size = 2
[u3.[ch]]
indent_size = 2
[unicode-utils.[ch]]
indent_size = 2
[ws_getopt.[ch]]
indent_style = tab
indent_size = tab

View File

@ -13,22 +13,22 @@
#include "unicode-utils.h"
int ws_utf8_seqlen[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x00...0x0f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x10...0x1f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x20...0x2f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x30...0x3f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x40...0x4f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x50...0x5f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x60...0x6f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x70...0x7f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80...0x8f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90...0x9f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0...0xaf */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0...0xbf */
0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xc0...0xcf */
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xd0...0xdf */
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, /* 0xe0...0xef */
4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, /* 0xf0...0xff */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x00...0x0f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x10...0x1f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x20...0x2f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x30...0x3f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x40...0x4f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x50...0x5f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x60...0x6f */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0x70...0x7f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80...0x8f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90...0x9f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0...0xaf */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0...0xbf */
0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xc0...0xcf */
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xd0...0xdf */
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, /* 0xe0...0xef */
4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, /* 0xf0...0xff */
};
/* Given a pointer and a length, validates a string of bytes as UTF-8.
@ -244,109 +244,105 @@ ws_utf8_make_valid(wmem_allocator_t *scope, const guint8 *ptr, ssize_t length)
const wchar_t *
utf_8to16(const char *utf8str)
{
static wchar_t *utf16buf[3];
static int utf16buf_len[3];
static int idx;
static wchar_t *utf16buf[3];
static int utf16buf_len[3];
static int idx;
if (utf8str == NULL)
return NULL;
if (utf8str == NULL)
return NULL;
idx = (idx + 1) % 3;
idx = (idx + 1) % 3;
/*
* Allocate the buffer if it's not already allocated.
*/
if (utf16buf[idx] == NULL) {
utf16buf_len[idx] = INITIAL_UTFBUF_SIZE;
utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t));
}
while (MultiByteToWideChar(CP_UTF8, 0, utf8str,
-1, NULL, 0) >= utf16buf_len[idx]) {
/*
* Double the buffer's size if it's not big enough.
* The size of the buffer starts at 128, so doubling its size
* adds at least another 128 bytes, which is more than enough
* for one more character plus a terminating '\0'.
* Allocate the buffer if it's not already allocated.
*/
utf16buf_len[idx] *= 2;
utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t));
}
if (utf16buf[idx] == NULL) {
utf16buf_len[idx] = INITIAL_UTFBUF_SIZE;
utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t));
}
if (MultiByteToWideChar(CP_UTF8, 0, utf8str,
-1, utf16buf[idx], utf16buf_len[idx]) == 0)
return NULL;
while (MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, NULL, 0) >= utf16buf_len[idx]) {
/*
* Double the buffer's size if it's not big enough.
* The size of the buffer starts at 128, so doubling its size
* adds at least another 128 bytes, which is more than enough
* for one more character plus a terminating '\0'.
*/
utf16buf_len[idx] *= 2;
utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t));
}
return utf16buf[idx];
if (MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, utf16buf[idx], utf16buf_len[idx]) == 0)
return NULL;
return utf16buf[idx];
}
void
utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt, ...)
{
va_list ap;
gchar* dst;
va_list ap;
gchar* dst;
va_start(ap,fmt);
dst = ws_strdup_vprintf(fmt, ap);
va_end(ap);
va_start(ap,fmt);
dst = ws_strdup_vprintf(fmt, ap);
va_end(ap);
StringCchPrintf(utf16buf, utf16buf_len, _T("%s"), utf_8to16(dst));
StringCchPrintf(utf16buf, utf16buf_len, _T("%s"), utf_8to16(dst));
g_free(dst);
g_free(dst);
}
/* Convert from UTF-16 to UTF-8. */
gchar *
utf_16to8(const wchar_t *utf16str)
{
static gchar *utf8buf[3];
static int utf8buf_len[3];
static int idx;
static gchar *utf8buf[3];
static int utf8buf_len[3];
static int idx;
if (utf16str == NULL)
return NULL;
if (utf16str == NULL)
return NULL;
idx = (idx + 1) % 3;
idx = (idx + 1) % 3;
/*
* Allocate the buffer if it's not already allocated.
*/
if (utf8buf[idx] == NULL) {
utf8buf_len[idx] = INITIAL_UTFBUF_SIZE;
utf8buf[idx] = g_malloc(utf8buf_len[idx]);
}
while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
/*
* Double the buffer's size if it's not big enough.
* The size of the buffer starts at 128, so doubling its size
* adds at least another 128 bytes, which is more than enough
* for one more character plus a terminating '\0'.
*/
utf8buf_len[idx] *= 2;
utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
}
* Allocate the buffer if it's not already allocated.
*/
if (utf8buf[idx] == NULL) {
utf8buf_len[idx] = INITIAL_UTFBUF_SIZE;
utf8buf[idx] = g_malloc(utf8buf_len[idx]);
}
if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)
return NULL;
while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1, NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
/*
* Double the buffer's size if it's not big enough.
* The size of the buffer starts at 128, so doubling its size
* adds at least another 128 bytes, which is more than enough
* for one more character plus a terminating '\0'.
*/
utf8buf_len[idx] *= 2;
utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
}
return utf8buf[idx];
if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1, utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)
return NULL;
return utf8buf[idx];
}
/* Convert our argument list from UTF-16 to UTF-8. */
char **
arg_list_utf_16to8(int argc, wchar_t *wc_argv[]) {
char **argv;
int i;
char **argv;
int i;
argv = (char **) g_malloc((argc + 1) * sizeof(char *));
for (i = 0; i < argc; i++) {
argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
}
argv[argc] = NULL;
return argv;
argv = (char **)g_malloc((argc + 1) * sizeof(char *));
for (i = 0; i < argc; i++) {
argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
}
argv[argc] = NULL;
return argv;
}
#endif
@ -354,12 +350,12 @@ arg_list_utf_16to8(int argc, wchar_t *wc_argv[]) {
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/

View File

@ -35,19 +35,19 @@ extern "C" {
#endif
#define _CHECK_UTF_8(level, str, len) \
do { \
const char *__uni_endptr; \
if (DEBUG_UTF_8_ENABLED && (str) != NULL && \
!g_utf8_validate(str, len, &__uni_endptr)) { \
ws_log_utf8(str, len, __uni_endptr); \
} \
} while (0)
do { \
const char *__uni_endptr; \
if (DEBUG_UTF_8_ENABLED && (str) != NULL && \
!g_utf8_validate(str, len, &__uni_endptr)) { \
ws_log_utf8(str, len, __uni_endptr); \
} \
} while (0)
#define WS_UTF_8_CHECK(str, len) \
_CHECK_UTF_8(LOG_LEVEL_DEBUG, str, len)
_CHECK_UTF_8(LOG_LEVEL_DEBUG, str, len)
#define WS_UTF_8_DEBUG_HERE(str, len) \
_CHECK_UTF_8(LOG_LEVEL_ECHO, str, len)
_CHECK_UTF_8(LOG_LEVEL_ECHO, str, len)
WSUTIL_EXPORT
int ws_utf8_seqlen[256];
@ -89,8 +89,8 @@ const wchar_t * utf_8to16(const char *utf8str);
* @param fmt A standard printf() format string
*/
WS_DLL_PUBLIC
void utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt,
...) G_GNUC_PRINTF(3, 4);
void utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt, ...)
G_GNUC_PRINTF(3, 4);
/** Given a UTF-16 string, convert it to UTF-8. This is meant to be used
* to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
@ -110,7 +110,7 @@ gchar * utf_16to8(const wchar_t *utf16str);
* @param argv The argument values (vector).
*/
WS_DLL_PUBLIC
char ** arg_list_utf_16to8(int argc, wchar_t *wc_argv[]);
char **arg_list_utf_16to8(int argc, wchar_t *wc_argv[]);
#endif /* _WIN32 */
@ -119,11 +119,11 @@ char ** arg_list_utf_16to8(int argc, wchar_t *wc_argv[]);
*/
#define IS_LEAD_SURROGATE(uchar2) \
((uchar2) >= 0xd800 && (uchar2) < 0xdc00)
((uchar2) >= 0xd800 && (uchar2) < 0xdc00)
#define IS_TRAIL_SURROGATE(uchar2) \
((uchar2) >= 0xdc00 && (uchar2) < 0xe000)
((uchar2) >= 0xdc00 && (uchar2) < 0xe000)
#define SURROGATE_VALUE(lead, trail) \
(((((lead) - 0xd800) << 10) | ((trail) - 0xdc00)) + 0x10000)
(((((lead) - 0xd800) << 10) | ((trail) - 0xdc00)) + 0x10000)
#ifdef __cplusplus
}