Freeciv-3.2
Loading...
Searching...
No Matches
Macros | Functions | Variables
fc_utf8.c File Reference
#include <stdarg.h>
#include <string.h>
#include "log.h"
#include "mem.h"
#include "support.h"
#include "fc_utf8.h"

Go to the source code of this file.

Macros

#define FC_UTF8_CHAR_SIZE(utf8_char)    fc_utf8_char_size[*(unsigned char *) utf8_char]
 
#define FC_UTF8_REP_CHAR   "\xef\xbf\xbd" /* U+FFFD. */
 

Functions

static bool base_fc_utf8_char_validate (const char *utf8_char, char size)
 
static size_t base_fc_utf8_strlcpy_trunc (char *dest, const char *src, size_t n)
 
static size_t base_fc_utf8_strlcpy_rep (char *dest, const char *src, size_t n)
 
bool fc_utf8_char_validate (const char *utf8_char)
 
const charfc_utf8_find_next_char (const char *utf8_char)
 
const charfc_utf8_find_prev_char (const char *utf8_char, const char *utf8_string)
 
bool fc_utf8_validate (const char *utf8_string, const char **end)
 
bool fc_utf8_validate_len (const char *utf8_string, size_t byte_len, const char **end)
 
charfc_utf8_validate_trunc (char *utf8_string)
 
charfc_utf8_validate_trunc_len (char *utf8_string, size_t byte_len)
 
charfc_utf8_validate_trunc_dup (const char *utf8_string)
 
charfc_utf8_validate_rep_len (char *utf8_string, size_t byte_len)
 
charfc_utf8_validate_rep_dup (const char *utf8_string)
 
size_t fc_utf8_strlen (const char *utf8_string)
 
size_t fc_utf8_strlcpy_trunc (char *dest, const char *src, size_t n)
 
size_t fc_utf8_strlcpy_rep (char *dest, const char *src, size_t n)
 
size_t fc_utf8_strlcat_trunc (char *dest, const char *src, size_t n)
 
size_t fc_utf8_strlcat_rep (char *dest, const char *src, size_t n)
 
int fc_utf8_snprintf_trunc (char *str, size_t n, const char *format,...)
 
int fc_utf8_snprintf_rep (char *str, size_t n, const char *format,...)
 
int fc_utf8_vsnprintf_trunc (char *str, size_t n, const char *format, va_list args)
 
int fc_utf8_vsnprintf_rep (char *str, size_t n, const char *format, va_list args)
 
int cat_utf8_snprintf_trunc (char *str, size_t n, const char *format,...)
 
int cat_utf8_snprintf_rep (char *str, size_t n, const char *format,...)
 

Variables

const char fc_utf8_skip [256]
 
static const char fc_utf8_char_size [256]
 

Macro Definition Documentation

◆ FC_UTF8_CHAR_SIZE

#define FC_UTF8_CHAR_SIZE (   utf8_char)     fc_utf8_char_size[*(unsigned char *) utf8_char]

Definition at line 79 of file fc_utf8.c.

◆ FC_UTF8_REP_CHAR

#define FC_UTF8_REP_CHAR   "\xef\xbf\xbd" /* U+FFFD. */

Definition at line 82 of file fc_utf8.c.

Function Documentation

◆ base_fc_utf8_char_validate()

static bool base_fc_utf8_char_validate ( const char utf8_char,
char  size 
)
inlinestatic

Returns TRUE if the character beginning at the pointer 'utf8_char' of size 'size' is a valid UTF-8 character.

Definition at line 89 of file fc_utf8.c.

Referenced by fc_utf8_char_validate(), fc_utf8_validate(), fc_utf8_validate_len(), and fc_utf8_validate_rep_dup().

◆ base_fc_utf8_strlcpy_rep()

static size_t base_fc_utf8_strlcpy_rep ( char dest,
const char src,
size_t  n 
)
inlinestatic

UTF-8-safe variant of fc_strlcpy() base function.

Definition at line 129 of file fc_utf8.c.

Referenced by fc_utf8_strlcat_rep(), fc_utf8_strlcpy_rep(), fc_utf8_validate_rep_dup(), and fc_utf8_validate_rep_len().

◆ base_fc_utf8_strlcpy_trunc()

static size_t base_fc_utf8_strlcpy_trunc ( char dest,
const char src,
size_t  n 
)
inlinestatic

UTF-8-safe variant of fc_strlcpy() base function.

Definition at line 110 of file fc_utf8.c.

Referenced by fc_utf8_strlcat_trunc(), and fc_utf8_strlcpy_trunc().

◆ cat_utf8_snprintf_rep()

int cat_utf8_snprintf_rep ( char str,
size_t  n,
const char format,
  ... 
)

This is a variant of cat_snprintf() to ensure the result will be a valid UTF-8 string. Unlike cat_utf8_snprintf_trunc(), it replaces the invalid characters by the replacement character, instead of truncating the string.

NB: This function doesn't perform anything on the already edited part of the string 'str', which can contain invalid UTF-8 characters.

See also cat_snprintf(), cat_utf8_snprintf_trunc().

Definition at line 634 of file fc_utf8.c.

◆ cat_utf8_snprintf_trunc()

int cat_utf8_snprintf_trunc ( char str,
size_t  n,
const char format,
  ... 
)

This is a variant of cat_snprintf() to ensure the result will be a valid UTF-8 string. It truncates the string at the first UTF-8 invalid character.

NB: This function doesn't perform anything on the already edited part of the string 'str', which can contain invalid UTF-8 characters.

See also cat_snprintf(), cat_utf8_snprintf_rep().

Definition at line 606 of file fc_utf8.c.

◆ fc_utf8_char_validate()

bool fc_utf8_char_validate ( const char utf8_char)

Returns TRUE if the character beginning at the pointer 'utf8_char' is a valid UTF-8 character.

Definition at line 193 of file fc_utf8.c.

◆ fc_utf8_find_next_char()

const char * fc_utf8_find_next_char ( const char utf8_char)

Jump to next UTF-8 character start.

NB: This function can return a invalid UTF-8 character. Check with fc_utf8_char_validate() to ensure.

Definition at line 204 of file fc_utf8.c.

Referenced by base_fc_utf8_strlcpy_rep().

◆ fc_utf8_find_prev_char()

const char * fc_utf8_find_prev_char ( const char utf8_char,
const char utf8_string 
)

Jump to previous UTF-8 character start in the limit of the 'utf8_string' pointer. If no character is found, returns 'utf8_string'.

NB: This function can return a invalid UTF-8 character. Check with fc_utf8_char_validate() to ensure.

Definition at line 220 of file fc_utf8.c.

◆ fc_utf8_snprintf_rep()

int fc_utf8_snprintf_rep ( char str,
size_t  n,
const char format,
  ... 
)

This is a variant of fc_snprintf() to ensure the result will be a valid UTF-8 string. Unlike fc_utf8_snprintf_trunc(), it replaces the invalid characters by the replacement character, instead of truncating the string.

See also fc_snprintf(), fc_utf8_snprintf_trunc().

Definition at line 534 of file fc_utf8.c.

◆ fc_utf8_snprintf_trunc()

int fc_utf8_snprintf_trunc ( char str,
size_t  n,
const char format,
  ... 
)

This is a variant of fc_snprintf() to ensure the result will be a valid UTF-8 string. It truncates the string at the first UTF-8 invalid character.

See also fc_snprintf(), fc_utf8_snprintf_rep().

Definition at line 515 of file fc_utf8.c.

◆ fc_utf8_strlcat_rep()

size_t fc_utf8_strlcat_rep ( char dest,
const char src,
size_t  n 
)

This is a variant of fc_strlcat() to ensure the result will be a valid UTF-8 string. Unlike fc_utf8_strlcat_trunc(), it replaces the invalid characters by the replacement character, instead of truncating the string.

NB: This function doesn't perform anything on the already edited part of the string 'dest', which can contain invalid UTF-8 characters.

See also fc_strlcat(), fc_utf8_strlcat_trunc().

Definition at line 496 of file fc_utf8.c.

◆ fc_utf8_strlcat_trunc()

size_t fc_utf8_strlcat_trunc ( char dest,
const char src,
size_t  n 
)

This is a variant of fc_strlcat() to ensure the result will be a valid UTF-8 string. It truncates the string at the first UTF-8 invalid character.

NB: This function doesn't perform anything on the already edited part of the string 'dest', which can contain invalid UTF-8 characters.

See also fc_strlcat(), fc_utf8_strlcat_rep().

Definition at line 474 of file fc_utf8.c.

◆ fc_utf8_strlcpy_rep()

size_t fc_utf8_strlcpy_rep ( char dest,
const char src,
size_t  n 
)

This is a variant of fc_strlcpy() to ensure the result will be a valid UTF-8 string. Unlike fc_utf8_strlcpy_trunc(), it replaces the invalid characters by the replacement character, instead of truncating the string.

See also fc_strlcpy(), fc_utf8_strlcpy_trunc().

Definition at line 457 of file fc_utf8.c.

◆ fc_utf8_strlcpy_trunc()

size_t fc_utf8_strlcpy_trunc ( char dest,
const char src,
size_t  n 
)

This is a variant of fc_strlcpy() to ensure the result will be a valid UTF-8 string. It truncates the string at the first UTF-8 invalid character.

See also fc_strlcpy(), fc_utf8_strlcpy_rep().

Definition at line 443 of file fc_utf8.c.

◆ fc_utf8_strlen()

size_t fc_utf8_strlen ( const char utf8_string)

Returns the number of characters in the string 'utf8_string'. To know the number of used bytes, used strlen() instead.

NB: 'utf8_string' must be UTF-8 valid (see fc_utf8_validate()), or the behaviour of this function will be unknown.

Definition at line 425 of file fc_utf8.c.

◆ fc_utf8_validate()

bool fc_utf8_validate ( const char utf8_string,
const char **  end 
)

Returns TRUE if the string 'utf8_string' contains only valid UTF-8 characters. If 'end' is not NULL, the end of the valid string will be stored there, even if it returns TRUE.

See also fc_utf8_validate_len().

Definition at line 239 of file fc_utf8.c.

Referenced by fc_utf8_validate_trunc(), fc_utf8_validate_trunc_dup(), fc_utf8_vsnprintf_rep(), and fc_utf8_vsnprintf_trunc().

◆ fc_utf8_validate_len()

bool fc_utf8_validate_len ( const char utf8_string,
size_t  byte_len,
const char **  end 
)

Returns TRUE if the string 'utf8_string' contains only valid UTF-8 characters in the limit of the length (in bytes) 'byte_len'. If 'end' is not NULL, the end of the valid string will be stored there, even if it returns TRUE.

See also fc_utf8_validate().

Definition at line 268 of file fc_utf8.c.

Referenced by base_fc_utf8_strlcpy_rep(), base_fc_utf8_strlcpy_trunc(), and fc_utf8_validate_trunc_len().

◆ fc_utf8_validate_rep_dup()

char * fc_utf8_validate_rep_dup ( const char utf8_string)

Duplicate 'utf8_string' and replace all invalid characters with the replacement character.

See also fc_utf8_validate_rep_len(), and fc_utf8_validate_trunc_dup().

Definition at line 386 of file fc_utf8.c.

◆ fc_utf8_validate_rep_len()

char * fc_utf8_validate_rep_len ( char utf8_string,
size_t  byte_len 
)

Transform 'utf8_string' with replacing all invalid characters with the replacement character in the limit of 'byte_len', truncate the last character. Returns 'utf8_string'.

See also fc_utf8_validate_len(), fc_utf8_validate_trunc(), and fc_utf8_validate_rep_dup().

Definition at line 368 of file fc_utf8.c.

Referenced by fc_utf8_vsnprintf_rep().

◆ fc_utf8_validate_trunc()

char * fc_utf8_validate_trunc ( char utf8_string)

Truncate the string 'utf8_string' at the first invalid UTF-8 character. Returns 'utf8_string'.

See also fc_utf8_validate(), fc_utf8_validate_trunc_len(), and fc_utf8_validate_trunc_dup().

Definition at line 309 of file fc_utf8.c.

◆ fc_utf8_validate_trunc_dup()

char * fc_utf8_validate_trunc_dup ( const char utf8_string)

Duplicate the truncation of the string 'utf8_string' at the first invalid UTF-8 character.

See also fc_utf8_validate_trunc(), fc_utf8_validate_trunc_len(), and fc_utf8_validate_rep_dup().

Definition at line 345 of file fc_utf8.c.

◆ fc_utf8_validate_trunc_len()

char * fc_utf8_validate_trunc_len ( char utf8_string,
size_t  byte_len 
)

Truncate the string 'utf8_string' at the first invalid UTF-8 character in the limit (in bytes) of 'byte_len'. Returns 'utf8_string'.

See also fc_utf8_validate_trunc(), fc_utf8_validate_trunc_dup(), and fc_utf8_validate_rep_len().

Definition at line 327 of file fc_utf8.c.

◆ fc_utf8_vsnprintf_rep()

int fc_utf8_vsnprintf_rep ( char str,
size_t  n,
const char format,
va_list  args 
)

This is a variant of fc_vsnprintf() to ensure the result will be a valid UTF-8 string. Unlike fc_utf8_vsnprintf_trunc(), it replaces the invalid characters by the replacement character, instead of truncating the string.

See also fc_vsnprintf(), fc_utf8_vsnprintf_trunc().

Definition at line 578 of file fc_utf8.c.

Referenced by cat_utf8_snprintf_rep(), and fc_utf8_snprintf_rep().

◆ fc_utf8_vsnprintf_trunc()

int fc_utf8_vsnprintf_trunc ( char str,
size_t  n,
const char format,
va_list  args 
)

This is a variant of fc_vsnprintf() to ensure the result will be a valid UTF-8 string. It truncates the string at the first UTF-8 invalid character.

See also fc_vsnprintf(), fc_utf8_vsnprintf_rep().

Definition at line 552 of file fc_utf8.c.

Referenced by cat_utf8_snprintf_trunc(), fc_utf8_snprintf_trunc(), and send_chat_printf().

Variable Documentation

◆ fc_utf8_char_size

const char fc_utf8_char_size[256]
static
Initial value:
= {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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