Skip to content

Commit

Permalink
add utf8_mbstoc32s/utf8_c32stombs/utf8_c16stoc32s/utf8_c32stoc16s
Browse files Browse the repository at this point in the history
  • Loading branch information
mbuilov committed Jun 17, 2020
1 parent 9b73cdb commit 2f998a5
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
76 changes: 76 additions & 0 deletions libutf16/utf8_cstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,82 @@ A_Success(return != A_Size_t(-1))
#endif
size_t utf8_c16stombs(utf8_char_t dst[/*n*/], const utf16_char_t *const src, size_t n);

/* mbstowcs (3) */
/* returns:
-1 on error (errno == EILSEQ)
dst != NULL:
number of utf32 characters stored, not counting terminating nul, which is not written
if buffer is too small. Use red zone in output buffer of at least 1 utf32 character
to check if buffer size is enough to store all characters, including terminating nul
character (only nul character can be written to red zone).
dst == NULL:
size of buffer required to store all utf32 characters, not counting terminating nul */
#ifdef SAL_DEFS_H_INCLUDED /* include "sal_defs.h" for the annotations */
A_Check_return
A_Nonnull_arg(2)
A_At(dst, A_Out_writes_opt(n))
A_At(src, A_In_z)
A_Success(return != A_Size_t(-1))
#endif
size_t utf8_mbstoc32s(utf32_char_t dst[/*n*/], const utf8_char_t *const src, size_t n);

/* wcstombs (3) */
/* returns:
-1 on error (errno == EILSEQ)
dst != NULL:
number of utf8 bytes stored, not counting terminating nul, which is not written
if buffer is too small. Use red zone in output buffer of at least UTF8_MAX_LEN
utf8 bytes to check if buffer size is enough to store all characters, including
terminating nul character (only nul character can be written to red zone).
dst == NULL:
size of buffer required to store all utf8 bytes, not counting terminating nul */
#ifdef SAL_DEFS_H_INCLUDED /* include "sal_defs.h" for the annotations */
A_Check_return
A_Nonnull_arg(2)
A_At(dst, A_Out_writes_opt(n))
A_At(src, A_In_z)
A_Success(return != A_Size_t(-1))
#endif
size_t utf8_c32stombs(utf8_char_t dst[/*n*/], const utf32_char_t *const src, size_t n);

/* mbstowcs (3) */
/* returns:
-1 on error (errno == EILSEQ)
dst != NULL:
number of utf32 characters stored, not counting terminating nul, which is not written
if buffer is too small. Use red zone in output buffer of at least 1 utf32 character
to check if buffer size is enough to store all characters, including terminating nul
character (only nul character can be written to red zone).
dst == NULL:
size of buffer required to store all utf32 characters, not counting terminating nul */
#ifdef SAL_DEFS_H_INCLUDED /* include "sal_defs.h" for the annotations */
A_Check_return
A_Nonnull_arg(2)
A_At(dst, A_Out_writes_opt(n))
A_At(src, A_In_z)
A_Success(return != A_Size_t(-1))
#endif
size_t utf8_c16stoc32s(utf32_char_t dst[/*n*/], const utf16_char_t *const src, size_t n);

/* wcstombs (3) */
/* returns:
-1 on error (errno == EILSEQ)
dst != NULL:
number of utf16 characters stored, not counting terminating nul, which is not written
if buffer is too small. Use red zone in output buffer of at least 2 utf16 characters
to check if buffer size is enough to store all characters, including terminating nul
character (only nul character can be written to red zone).
dst == NULL:
size of buffer required to store all utf16 characters, not counting terminating nul */
#ifdef SAL_DEFS_H_INCLUDED /* include "sal_defs.h" for the annotations */
A_Check_return
A_Nonnull_arg(2)
A_At(dst, A_Out_writes_opt(n))
A_At(src, A_In_z)
A_Success(return != A_Size_t(-1))
#endif
size_t utf8_c32stoc16s(utf16_char_t dst[/*n*/], const utf32_char_t *const src, size_t n);

#ifdef __cplusplus
}
#endif
Expand Down
88 changes: 88 additions & 0 deletions src/utf8_cstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "libutf16/utf8_cstd.h"
#include "libutf16/utf8_to_utf16.h"
#include "libutf16/utf16_to_utf8.h"
#include "libutf16/utf8_to_utf32.h"
#include "libutf16/utf32_to_utf8.h"
#include "libutf16/utf16_to_utf32.h"
#include "libutf16/utf32_to_utf16.h"
#include "libutf16/utf8_to_utf16_one.h"
#include "libutf16/utf16_to_utf8_one.h"

Expand Down Expand Up @@ -375,3 +379,87 @@ size_t utf8_c16stombs(utf8_char_t dst[/*n*/], const utf16_char_t *const src, siz
return (size_t)(b - dst);
}
}

A_Use_decl_annotations
size_t utf8_mbstoc32s(utf32_char_t dst[/*n*/], const utf8_char_t *const src, size_t n)
{
if (!dst)
n = 0;
else if (!n)
return 0;
{
const utf8_char_t *q = src;
utf32_char_t *b = dst;
const size_t sz = utf8_to_utf32_z(&q, &b, n);
if (!sz) {
errno = EILSEQ;
return (size_t)-1;
}
if (sz <= n || !n)
return sz - 1; /* don't count terminating nul */
return (size_t)(b - dst);
}
}

A_Use_decl_annotations
size_t utf8_c32stombs(utf8_char_t dst[/*n*/], const utf32_char_t *const src, size_t n)
{
if (!dst)
n = 0;
else if (!n)
return 0;
{
const utf32_char_t *w = src;
utf8_char_t *b = dst;
const size_t sz = utf32_to_utf8_z(&w, &b, n);
if (!sz) {
errno = EILSEQ;
return (size_t)-1;
}
if (sz <= n || !n)
return sz - 1; /* don't count terminating nul */
return (size_t)(b - dst);
}
}

A_Use_decl_annotations
size_t utf8_c16stoc32s(utf32_char_t dst[/*n*/], const utf16_char_t *const src, size_t n)
{
if (!dst)
n = 0;
else if (!n)
return 0;
{
const utf16_char_t *q = src;
utf32_char_t *b = dst;
const size_t sz = utf16_to_utf32_z(&q, &b, n);
if (!sz) {
errno = EILSEQ;
return (size_t)-1;
}
if (sz <= n || !n)
return sz - 1; /* don't count terminating nul */
return (size_t)(b - dst);
}
}

A_Use_decl_annotations
size_t utf8_c32stoc16s(utf16_char_t dst[/*n*/], const utf32_char_t *const src, size_t n)
{
if (!dst)
n = 0;
else if (!n)
return 0;
{
const utf32_char_t *w = src;
utf16_char_t *b = dst;
const size_t sz = utf32_to_utf16_z(&w, &b, n);
if (!sz) {
errno = EILSEQ;
return (size_t)-1;
}
if (sz <= n || !n)
return sz - 1; /* don't count terminating nul */
return (size_t)(b - dst);
}
}

0 comments on commit 2f998a5

Please sign in to comment.