Index: libc/src/uchar/CMakeLists.txt =================================================================== --- libc/src/uchar/CMakeLists.txt +++ libc/src/uchar/CMakeLists.txt @@ -32,4 +32,4 @@ HDRS c32rtomb.h #DEPENDS -) \ No newline at end of file +) Index: libc/src/uchar/c16rtomb.h =================================================================== --- libc/src/uchar/c16rtomb.h +++ libc/src/uchar/c16rtomb.h @@ -1,4 +1,5 @@ -//===----------------- Implementation header for c16rtomb -------------------===// +//===----------------- Implementation header for c16rtomb +//-------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -12,9 +13,9 @@ #include "include/uchar.h" namespace __llvm_libc { - - size_t c16rtomb(char * restrict s, char16_t c16, mbstate_t * restrict ps); - + +size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps); + } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_UCHAR_C16RTOMB_H Index: libc/src/uchar/c16rtomb.cpp =================================================================== --- libc/src/uchar/c16rtomb.cpp +++ libc/src/uchar/c16rtomb.cpp @@ -1,4 +1,5 @@ -//===-------------------- Implementation of c16rtomb -----------------------===// +//===-------------------- Implementation of c16rtomb +//-----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -11,36 +12,31 @@ #include "src/__support/common.h" namespace __llvm_libc { - - size_t LLVM_LIBC_ENTRYPOINT(c16rtomb)(char * restrict s, char16_t c16, mbstate_t * restrict ps) { - size_t StringSize = 0ULL; - /* - s = output string of UTF-8 code units - c16 = the non-surrogate pair UTF-16 code unit to convert to UTF-8 - ps = string position? - return value = size of s in bytes aka the number of bytes that had to be added - */ - if (c16 >= 0xD800 && c16 <= 0xDFFF) { - c16 = 0xFFFD; // Invalid Replacement Character - } - - if (C16 <= 0x7F) { - StringSize = 1; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = C16 & 0x7F; - } else if (C16 <= 0x7FF) { - StringSize = 2; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = 0xC0 | (C16 & ((0x1F << 6) >> 6)); - s[1] = 0x80 | (C16 & 0x3F); - } else if (C16 <= 0xFFFF) { - StringSize = 3; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = 0xE0 | (C16 & ((0x0F << 12) >> 12)); - s[1] = 0x80 | (C16 & ((0x3F << 6) >> 6)); - s[2] = 0x80 | (C16 & 0x3F); - } - return StringSize; - } - + +size_t LLVM_LIBC_ENTRYPOINT(c16rtomb)(char *restrict s, char16_t c16, + mbstate_t *restrict ps) { + size_t StringSize = 0; + if (c16 >= 0xD800 && c16 <= 0xDFFF) { + c16 = 0xFFFD; // Invalid Replacement Character + } + + if (C16 <= 0x7F) { + StringSize = 1; + s = new char(StringSize); + s[0] = C16 & 0x7F; + } else if (C16 <= 0x7FF) { + StringSize = 2; + s = new char(StringSize); + s[0] = 0xC0 | (C16 & ((0x1F << 6) >> 6)); + s[1] = 0x80 | (C16 & 0x3F); + } else if (C16 <= 0xFFFF) { + StringSize = 3; + s = new char(StringSize); + s[0] = 0xE0 | (C16 & ((0x0F << 12) >> 12)); + s[1] = 0x80 | (C16 & ((0x3F << 6) >> 6)); + s[2] = 0x80 | (C16 & 0x3F); + } + return StringSize; +} + } // namespace __llvm_libc Index: libc/src/uchar/c32rtomb.h =================================================================== --- libc/src/uchar/c32rtomb.h +++ libc/src/uchar/c32rtomb.h @@ -1,4 +1,5 @@ -//===----------------- Implementation header for c32rtomb -------------------===// +//===----------------- Implementation header for c32rtomb +//-------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -12,9 +13,9 @@ #include "include/uchar.h" namespace __llvm_libc { - - size_t c16rtomb(char * restrict s, char16_t c16, mbstate_t * restrict ps); - + +size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps); + } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_UCHAR_C16RTOMB_H Index: libc/src/uchar/c32rtomb.cpp =================================================================== --- libc/src/uchar/c32rtomb.cpp +++ libc/src/uchar/c32rtomb.cpp @@ -1,4 +1,5 @@ -//===-------------------- Implementation of c32rtomb -----------------------===// +//===-------------------- Implementation of c32rtomb +//-----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -11,33 +12,34 @@ #include "src/__support/common.h" namespace __llvm_libc { - - size_t LLVM_LIBC_ENTRYPOINT(c32rtomb)(char * restrict s, char32_t c32, mbstate_t * restrict ps) { - size_t StringSize = 0ULL; - if (c32 <= 0x7F) { - StringSize = 1; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = c32 & 0x7F; - } else if (c32 <= 0x7FF) { - StringSize = 2; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = 0xC0 | (c32 & ((0x1F << 6) >> 6)); - s[1] = 0x80 | (c32 & 0x3F); - } else if (c32 <= 0xFFFF) { - StringSize = 3; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = 0xE0 | (c32 & ((0x0F << 12) >> 12)); - s[1] = 0x80 | (c32 & ((0x3F << 6) >> 6)); - s[2] = 0x80 | (c32 & 0x3F); - } else if (c32 <= 0x10FFFF) { - StringSize = 4; - s = calloc(StringSize, sizeof(uint8_t)); - s[0] = 0xF0 | (c32 & 0x1C0000) >> 18; - s[1] = 0x80 | (c32 & 0x3F000) >> 12; - s[2] = 0x80 | (c32 & 0xFC0) >> 6; - s[3] = 0x80 | (c32 & 0x3F); - } - return StringSize; - } - + +size_t LLVM_LIBC_ENTRYPOINT(c32rtomb)(char *restrict s, char32_t c32, + mbstate_t *restrict ps) { + size_t StringSize = 0; + if (c32 <= 0x7F) { + StringSize = 1; + s = new char(StringSize); + s[0] = c32 & 0x7F; + } else if (c32 <= 0x7FF) { + StringSize = 2; + s = new char(StringSize); + s[0] = 0xC0 | (c32 & ((0x1F << 6) >> 6)); + s[1] = 0x80 | (c32 & 0x3F); + } else if (c32 <= 0xFFFF) { + StringSize = 3; + s = new char(StringSize); + s[0] = 0xE0 | (c32 & ((0x0F << 12) >> 12)); + s[1] = 0x80 | (c32 & ((0x3F << 6) >> 6)); + s[2] = 0x80 | (c32 & 0x3F); + } else if (c32 <= 0x10FFFF) { + StringSize = 4; + s = new char(StringSize); + s[0] = 0xF0 | (c32 & 0x1C0000) >> 18; + s[1] = 0x80 | (c32 & 0x3F000) >> 12; + s[2] = 0x80 | (c32 & 0xFC0) >> 6; + s[3] = 0x80 | (c32 & 0x3F); + } + return StringSize; +} + } // namespace __llvm_libc Index: libc/src/uchar/mbrtoc16.h =================================================================== --- libc/src/uchar/mbrtoc16.h +++ libc/src/uchar/mbrtoc16.h @@ -1,4 +1,5 @@ -//===----------------- Implementation header for mbrtoc16 -------------------===// +//===----------------- Implementation header for mbrtoc16 +//-------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -12,9 +13,10 @@ #include "include/uchar.h" namespace __llvm_libc { - - size_t mbrtoc16(char16_t * restrict pc16, const char * restrict s, size_t n, mbstate_t * restrict ps); - + +size_t mbrtoc16(char16_t *restrict pc16, const char *restrict s, size_t n, + mbstate_t *restrict ps); + } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_UCHAR_MBRTOC16_H Index: libc/src/uchar/mbrtoc16.cpp =================================================================== --- libc/src/uchar/mbrtoc16.cpp +++ libc/src/uchar/mbrtoc16.cpp @@ -1,4 +1,5 @@ -//===-------------------- Implementation of mbrtoc16 -----------------------===// +//===-------------------- Implementation of mbrtoc16 +//-----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -11,43 +12,45 @@ #include "src/__support/common.h" namespace __llvm_libc { - - size_t LLVM_LIBC_ENTRYPOINT(mbrtoc16)(char16_t * restrict pc16, const char * restrict s, size_t n, mbstate_t * restrict ps) { - size_t StringSize = 0ULL; - char32_t Decoded = 0; - - switch (n) { - case 1: - Decoded = s[0] & 0x7F; - break; - case 2: - Decoded |= (s[0] & 0x1F) << 6; - Decoded |= (s[1] & 0x3F) << 0; - break; - case 3: - Decoded |= (s[0] & 0x0F) << 12; - Decoded |= (s[1] & 0x1F) << 6; - Decoded |= (s[2] & 0x1F) << 0; - break; - case 4: - Decoded |= (s[0] & 0x07) << 18; - Decoded |= (s[1] & 0x3F) << 12; - Decoded |= (s[2] & 0x3F) << 6; - Decoded |= (s[3] & 0x3F) << 0; - break; - } - - if (Decoded <= 0xFFFF) { - StringSize = 1; - pc16 = calloc(1, sizeof(char16_t)); - pc16[0] = Decoded & 0xFFFF; - } else if (Decoded <= 0x10FFFF) { - StringSize = 2; - pc16 = calloc(2, sizeof(char16_t)); - pc16[0] = 0xD800 + ((Decoded & 0xFFC00) >> 10); - pc16[1] = 0xDC00 + (Decoded & 0x3FF); - } - return StringSize; - } - + +size_t LLVM_LIBC_ENTRYPOINT(mbrtoc16)(char16_t *restrict pc16, + const char *restrict s, size_t n, + mbstate_t *restrict ps) { + size_t StringSize = 0; + char32_t Decoded = 0; + + switch (n) { + case 1: + Decoded = s[0] & 0x7F; + break; + case 2: + Decoded |= (s[0] & 0x1F) << 6; + Decoded |= (s[1] & 0x3F) << 0; + break; + case 3: + Decoded |= (s[0] & 0x0F) << 12; + Decoded |= (s[1] & 0x1F) << 6; + Decoded |= (s[2] & 0x1F) << 0; + break; + case 4: + Decoded |= (s[0] & 0x07) << 18; + Decoded |= (s[1] & 0x3F) << 12; + Decoded |= (s[2] & 0x3F) << 6; + Decoded |= (s[3] & 0x3F) << 0; + break; + } + + if (Decoded <= 0xFFFF) { + StringSize = 1; + pc16 = new char16_t(StringSize); + pc16[0] = Decoded & 0xFFFF; + } else if (Decoded <= 0x10FFFF) { + StringSize = 2; + pc16 = new char16_t(StringSize); + pc16[0] = 0xD800 + ((Decoded & 0xFFC00) >> 10); + pc16[1] = 0xDC00 + (Decoded & 0x3FF); + } + return StringSize; +} + } // namespace __llvm_libc Index: libc/src/uchar/mbrtoc32.h =================================================================== --- libc/src/uchar/mbrtoc32.h +++ libc/src/uchar/mbrtoc32.h @@ -1,4 +1,5 @@ -//===----------------- Implementation header for mbrtoc32 -------------------===// +//===----------------- Implementation header for mbrtoc32 +//-------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -12,9 +13,10 @@ #include "include/uchar.h" namespace __llvm_libc { - - size_t mbrtoc32(char32_t * restrict pc32, const char * restrict s, size_t n, mbstate_t * restrict ps); - + +size_t mbrtoc32(char32_t *restrict pc32, const char *restrict s, size_t n, + mbstate_t *restrict ps); + } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_UCHAR_MBRTOC32_H Index: libc/src/uchar/mbrtoc32.cpp =================================================================== --- libc/src/uchar/mbrtoc32.cpp +++ libc/src/uchar/mbrtoc32.cpp @@ -1,4 +1,5 @@ -//===-------------------- Implementation of mbrtoc32 -----------------------===// +//===-------------------- Implementation of mbrtoc32 +//-----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -11,32 +12,34 @@ #include "src/__support/common.h" namespace __llvm_libc { - - size_t LLVM_LIBC_ENTRYPOINT(mbrtoc32)(char32_t * restrict pc32, const char * restrict s, size_t n, mbstate_t * restrict ps) { - size_t StringSize = 1ULL; - char32_t Decoded = calloc(1, sizeof(char32_t)); - - switch (n) { - case 1: - pc32[0] = s[0] & 0x7F; - break; - case 2: - pc32[0] |= (s[0] & 0x1F) << 6; - pc32[0] |= (s[1] & 0x3F) << 0; - break; - case 3: - pc32[0] |= (s[0] & 0x0F) << 12; - pc32[0] |= (s[1] & 0x1F) << 6; - pc32[0] |= (s[2] & 0x1F) << 0; - break; - case 4: - pc32[0] |= (s[0] & 0x07) << 18; - pc32[0] |= (s[1] & 0x3F) << 12; - pc32[0] |= (s[2] & 0x3F) << 6; - pc32[0] |= (s[3] & 0x3F) << 0; - break; - } - return StringSize; - } - + +size_t LLVM_LIBC_ENTRYPOINT(mbrtoc32)(char32_t *restrict pc32, + const char *restrict s, size_t n, + mbstate_t *restrict ps) { + size_t StringSize = 1; + pc32 = new char32_t(StringSize); + + switch (n) { + case 1: + pc32[0] = s[0] & 0x7F; + break; + case 2: + pc32[0] |= (s[0] & 0x1F) << 6; + pc32[0] |= (s[1] & 0x3F) << 0; + break; + case 3: + pc32[0] |= (s[0] & 0x0F) << 12; + pc32[0] |= (s[1] & 0x1F) << 6; + pc32[0] |= (s[2] & 0x1F) << 0; + break; + case 4: + pc32[0] |= (s[0] & 0x07) << 18; + pc32[0] |= (s[1] & 0x3F) << 12; + pc32[0] |= (s[2] & 0x3F) << 6; + pc32[0] |= (s[3] & 0x3F) << 0; + break; + } + return StringSize; +} + } // namespace __llvm_libc