|
| 1 | +//===-- Definition of a class for mbstate_t and conversion -----*-- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_SRC___SUPPORT_STRING_CONVERTER_H |
| 10 | +#define LLVM_LIBC_SRC___SUPPORT_STRING_CONVERTER_H |
| 11 | + |
| 12 | +#include "hdr/types/char32_t.h" |
| 13 | +#include "hdr/types/char8_t.h" |
| 14 | +#include "hdr/types/size_t.h" |
| 15 | +#include "src/__support/common.h" |
| 16 | +#include "src/__support/error_or.h" |
| 17 | +#include "src/__support/wchar/character_converter.h" |
| 18 | +#include "src/__support/wchar/mbstate.h" |
| 19 | + |
| 20 | +namespace LIBC_NAMESPACE_DECL { |
| 21 | +namespace internal { |
| 22 | + |
| 23 | +template <typename T> class StringConverter { |
| 24 | +private: |
| 25 | + CharacterConverter cr; |
| 26 | + const T *src; |
| 27 | + size_t src_len; |
| 28 | + size_t src_idx; |
| 29 | + |
| 30 | + // # of pops we are allowed to perform (essentially size of the dest buffer) |
| 31 | + size_t num_to_write; |
| 32 | + |
| 33 | + ErrorOr<size_t> pushFullCharacter() { |
| 34 | + size_t num_pushed; |
| 35 | + for (num_pushed = 0; !cr.isFull() && src_idx + num_pushed < src_len; |
| 36 | + ++num_pushed) { |
| 37 | + int err = cr.push(src[src_idx + num_pushed]); |
| 38 | + if (err != 0) |
| 39 | + return Error(err); |
| 40 | + } |
| 41 | + |
| 42 | + // if we aren't able to read a full character from the source string |
| 43 | + if (src_idx + num_pushed == src_len && !cr.isFull()) { |
| 44 | + src_idx += num_pushed; |
| 45 | + return Error(-1); |
| 46 | + } |
| 47 | + |
| 48 | + return num_pushed; |
| 49 | + } |
| 50 | + |
| 51 | +public: |
| 52 | + StringConverter(const T *s, mbstate *ps, size_t dstlen, |
| 53 | + size_t srclen = SIZE_MAX) |
| 54 | + : cr(ps), src(s), src_len(srclen), src_idx(0), num_to_write(dstlen) {} |
| 55 | + |
| 56 | + // TODO: following functions are almost identical |
| 57 | + // look into templating CharacterConverter pop functions |
| 58 | + ErrorOr<char32_t> popUTF32() { |
| 59 | + if (cr.isEmpty() || src_idx == 0) { |
| 60 | + auto src_elements_read = pushFullCharacter(); |
| 61 | + if (!src_elements_read.has_value()) |
| 62 | + return Error(src_elements_read.error()); |
| 63 | + |
| 64 | + if (cr.sizeAsUTF32() > num_to_write) { |
| 65 | + cr.clear(); |
| 66 | + return Error(-1); |
| 67 | + } |
| 68 | + |
| 69 | + src_idx += src_elements_read.value(); |
| 70 | + } |
| 71 | + |
| 72 | + auto out = cr.pop_utf32(); |
| 73 | + if (out.has_value() && out.value() == L'\0') |
| 74 | + src_len = src_idx; |
| 75 | + |
| 76 | + num_to_write--; |
| 77 | + |
| 78 | + return out; |
| 79 | + } |
| 80 | + |
| 81 | + ErrorOr<char8_t> popUTF8() { |
| 82 | + if (cr.isEmpty() || src_idx == 0) { |
| 83 | + auto src_elements_read = pushFullCharacter(); |
| 84 | + if (!src_elements_read.has_value()) |
| 85 | + return Error(src_elements_read.error()); |
| 86 | + |
| 87 | + if (cr.sizeAsUTF8() > num_to_write) { |
| 88 | + cr.clear(); |
| 89 | + return Error(-1); |
| 90 | + } |
| 91 | + |
| 92 | + src_idx += src_elements_read.value(); |
| 93 | + } |
| 94 | + |
| 95 | + auto out = cr.pop_utf8(); |
| 96 | + if (out.has_value() && out.value() == '\0') |
| 97 | + src_len = src_idx; |
| 98 | + |
| 99 | + num_to_write--; |
| 100 | + |
| 101 | + return out; |
| 102 | + } |
| 103 | + |
| 104 | + size_t getSourceIndex() { return src_idx; } |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace internal |
| 108 | +} // namespace LIBC_NAMESPACE_DECL |
| 109 | + |
| 110 | +#endif // LLVM_LIBC_SRC___SUPPORT_STRING_CONVERTER_H |
0 commit comments