Skip to content

Commit

Permalink
utf8: improve const safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Jun 25, 2024
1 parent e2c3f3f commit 6abde99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/tf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ tf_func_strchr(ddb_tf_context_t *ctx, int argc, const uint16_t *arglens, const c
TF_EVAL_CHECK(len, ctx, args, arglens[0], out, outlen, fail_on_undef);

int32_t charpos;
char *pos = u8_strchr(out, needle, &charpos);
const char *pos = u8_strchr(out, needle, &charpos);

if (!pos) {
// 0 is used to indicate not found
Expand Down Expand Up @@ -1517,7 +1517,7 @@ tf_func_strrchr(ddb_tf_context_t *ctx, int argc, const uint16_t *arglens, const
uint32_t needle = u8_nextchar(str, &dummy);

int32_t acc = 0, charpos;
char *pos = out;
const char *pos = out;
do {
pos = u8_strchr(pos, needle, &charpos);
if (pos) {
Expand Down
16 changes: 8 additions & 8 deletions src/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int u8_toucs(uint32_t *dest, int32_t sz, const char *src, int32_t srcsz)
the NUL as well.
the destination string will never be bigger than the source string.
*/
int u8_toutf8(char *dest, int32_t sz, uint32_t *src, int32_t srcsz)
int u8_toutf8(char *dest, int32_t sz, const uint32_t *src, int32_t srcsz)
{
uint32_t ch;
int32_t i = 0;
Expand Down Expand Up @@ -199,7 +199,7 @@ int u8_offset(const char *str, int32_t charnum)
}

/* byte offset => charnum */
int u8_charnum(char *s, int32_t offset)
int u8_charnum(const char *s, int32_t offset)
{
int32_t charnum = 0, offs=0;

Expand All @@ -212,7 +212,7 @@ int u8_charnum(char *s, int32_t offset)
}

/* number of characters, not including a null terminator */
int u8_strlen(char *s)
int u8_strlen(const char *s)
{
int32_t count = 0;
int32_t i = 0;
Expand Down Expand Up @@ -448,7 +448,7 @@ int u8_escape(char *buf, int32_t sz, const char *src, int32_t escape_quotes)
return c;
}

char *u8_strchr(char *s, uint32_t ch, int32_t *charn)
const char *u8_strchr(const char *s, uint32_t ch, int32_t *charn)
{
int32_t i = 0, lasti=0;
uint32_t c;
Expand All @@ -465,7 +465,7 @@ char *u8_strchr(char *s, uint32_t ch, int32_t *charn)
return NULL;
}

char *u8_memchr(char *s, uint32_t ch, size_t sz, int32_t *charn)
const char *u8_memchr(const char *s, uint32_t ch, size_t sz, int32_t *charn)
{
int32_t i = 0, lasti=0;
uint32_t c;
Expand All @@ -490,7 +490,7 @@ char *u8_memchr(char *s, uint32_t ch, size_t sz, int32_t *charn)
return NULL;
}

int u8_is_locale_utf8(char *locale)
int u8_is_locale_utf8(const char *locale)
{
/* this code based on libutf8 */
const char* cp = locale;
Expand All @@ -509,7 +509,7 @@ int u8_is_locale_utf8(char *locale)
return 0;
}

int u8_vprintf(char *fmt, va_list ap)
int u8_vprintf(const char *fmt, va_list ap)
{
int32_t cnt, sz=0;
char *buf;
Expand All @@ -530,7 +530,7 @@ int u8_vprintf(char *fmt, va_list ap)
return cnt;
}

int u8_printf(char *fmt, ...)
int u8_printf(const char *fmt, ...)
{
int32_t cnt;
va_list args;
Expand Down
16 changes: 8 additions & 8 deletions src/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
int u8_toucs(uint32_t *dest, int32_t sz, const char *src, int32_t srcsz);

/* the opposite conversion */
int u8_toutf8(char *dest, int32_t sz, uint32_t *src, int32_t srcsz);
int u8_toutf8(char *dest, int32_t sz, const uint32_t *src, int32_t srcsz);

/* single character to UTF-8 */
int u8_wc_toutf8(char *dest, uint32_t ch);
Expand All @@ -53,7 +53,7 @@ int u8_wc_toutf8(char *dest, uint32_t ch);
int u8_offset(const char *str, int32_t charnum);

/* byte offset to character number */
int u8_charnum(char *s, int32_t offset);
int u8_charnum(const char *s, int32_t offset);

/* return next character, updating an index variable */
uint32_t u8_nextchar(const char *s, int32_t *i);
Expand Down Expand Up @@ -100,22 +100,22 @@ int hex_digit(char c);

/* return a pointer to the first occurrence of ch in s, or NULL if not
found. character index of found character returned in *charn. */
char *u8_strchr(char *s, uint32_t ch, int32_t *charn);
const char *u8_strchr(const char *s, uint32_t ch, int32_t *charn);

/* same as the above, but searches a buffer of a given size instead of
a NUL-terminated string. */
char *u8_memchr(char *s, uint32_t ch, size_t sz, int32_t *charn);
const char *u8_memchr(const char *s, uint32_t ch, size_t sz, int32_t *charn);

/* count the number of characters in a UTF-8 string */
int u8_strlen(char *s);
int u8_strlen(const char *s);

int u8_is_locale_utf8(char *locale);
int u8_is_locale_utf8(const char *locale);

/* printf where the format string and arguments may be in UTF-8.
you can avoid this function and just use ordinary printf() if the current
locale is UTF-8. */
int u8_vprintf(char *fmt, va_list ap);
int u8_printf(char *fmt, ...);
int u8_vprintf(const char *fmt, va_list ap);
int u8_printf(const char *fmt, ...);

// validate utf8 string
// returns 1 if valid, 0 otherwise
Expand Down

0 comments on commit 6abde99

Please sign in to comment.