Skip to content

Commit 9fe88da

Browse files
committed
added a helper to check if a string conversion has completed
1 parent 3a6ef8b commit 9fe88da

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

libc/src/__support/wchar/string_converter.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ template <typename T> class StringConverter {
2626
const T *src;
2727
size_t src_len;
2828
size_t src_idx;
29+
bool completed = false;
2930

3031
// # of pops we are allowed to perform (essentially size of the dest buffer)
3132
size_t num_to_write;
@@ -70,8 +71,10 @@ template <typename T> class StringConverter {
7071
}
7172

7273
auto out = cr.pop_utf32();
73-
if (out.has_value() && out.value() == L'\0')
74+
if (out.has_value() && out.value() == L'\0') {
75+
completed = true;
7476
src_len = src_idx;
77+
}
7578

7679
num_to_write--;
7780

@@ -93,15 +96,19 @@ template <typename T> class StringConverter {
9396
}
9497

9598
auto out = cr.pop_utf8();
96-
if (out.has_value() && out.value() == '\0')
99+
if (out.has_value() && out.value() == '\0') {
100+
completed = true;
97101
src_len = src_idx;
102+
}
98103

99104
num_to_write--;
100105

101106
return out;
102107
}
103108

104109
size_t getSourceIndex() { return src_idx; }
110+
111+
bool isConversionComplete() { return completed; }
105112
};
106113

107114
} // namespace internal

libc/test/src/__support/wchar/string_converter_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,31 @@ TEST(LlvmLibcStringConverterTest, UTF8To32) {
3838
ASSERT_TRUE(res.has_value());
3939
ASSERT_EQ(static_cast<int>(res.value()), 0x1f921);
4040
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 4);
41+
ASSERT_FALSE(sc.isConversionComplete());
4142

4243
res = sc.popUTF32();
4344
ASSERT_TRUE(res.has_value());
4445
ASSERT_EQ(static_cast<int>(res.value()), 0x2211);
4546
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 7);
47+
ASSERT_FALSE(sc.isConversionComplete());
4648

4749
res = sc.popUTF32();
4850
ASSERT_TRUE(res.has_value());
4951
ASSERT_EQ(static_cast<int>(res.value()), 0xff);
5052
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 9);
53+
ASSERT_FALSE(sc.isConversionComplete());
5154

5255
res = sc.popUTF32();
5356
ASSERT_TRUE(res.has_value());
5457
ASSERT_EQ(static_cast<int>(res.value()), 0x41);
5558
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 10);
59+
ASSERT_FALSE(sc.isConversionComplete());
5660

5761
res = sc.popUTF32();
5862
ASSERT_TRUE(res.has_value());
5963
ASSERT_EQ(static_cast<int>(res.value()), 0);
6064
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 11);
65+
ASSERT_TRUE(sc.isConversionComplete());
6166

6267
res = sc.popUTF32();
6368
ASSERT_FALSE(res.has_value());
@@ -79,60 +84,71 @@ TEST(LlvmLibcStringConverterTest, UTF32To8) {
7984
ASSERT_TRUE(res.has_value());
8085
ASSERT_EQ(static_cast<int>(res.value()), 0xF0);
8186
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 1);
87+
ASSERT_FALSE(sc.isConversionComplete());
8288

8389
res = sc.popUTF8();
8490
ASSERT_TRUE(res.has_value());
8591
ASSERT_EQ(static_cast<int>(res.value()), 0x9F);
8692
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 1);
93+
ASSERT_FALSE(sc.isConversionComplete());
8794

8895
res = sc.popUTF8();
8996
ASSERT_TRUE(res.has_value());
9097
ASSERT_EQ(static_cast<int>(res.value()), 0xA4);
9198
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 1);
99+
ASSERT_FALSE(sc.isConversionComplete());
92100

93101
res = sc.popUTF8();
94102
ASSERT_TRUE(res.has_value());
95103
ASSERT_EQ(static_cast<int>(res.value()), 0xA1);
96104
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 1);
105+
ASSERT_FALSE(sc.isConversionComplete());
97106

98107
// end of clown emoji, sigma symbol begins
99108
res = sc.popUTF8();
100109
ASSERT_TRUE(res.has_value());
101110
ASSERT_EQ(static_cast<int>(res.value()), 0xE2);
102111
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 2);
112+
ASSERT_FALSE(sc.isConversionComplete());
103113

104114
res = sc.popUTF8();
105115
ASSERT_TRUE(res.has_value());
106116
ASSERT_EQ(static_cast<int>(res.value()), 0x88);
107117
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 2);
118+
ASSERT_FALSE(sc.isConversionComplete());
108119

109120
res = sc.popUTF8();
110121
ASSERT_TRUE(res.has_value());
111122
ASSERT_EQ(static_cast<int>(res.value()), 0x91);
112123
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 2);
124+
ASSERT_FALSE(sc.isConversionComplete());
113125

114126
// end of sigma symbol, y with diaeresis begins
115127
res = sc.popUTF8();
116128
ASSERT_TRUE(res.has_value());
117129
ASSERT_EQ(static_cast<int>(res.value()), 0xC3);
118130
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 3);
131+
ASSERT_FALSE(sc.isConversionComplete());
119132

120133
res = sc.popUTF8();
121134
ASSERT_TRUE(res.has_value());
122135
ASSERT_EQ(static_cast<int>(res.value()), 0xBF);
123136
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 3);
137+
ASSERT_FALSE(sc.isConversionComplete());
124138

125139
// end of y with diaeresis, letter A begins
126140
res = sc.popUTF8();
127141
ASSERT_TRUE(res.has_value());
128142
ASSERT_EQ(static_cast<int>(res.value()), 0x41);
129143
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 4);
144+
ASSERT_FALSE(sc.isConversionComplete());
130145

131146
// null byte
132147
res = sc.popUTF8();
133148
ASSERT_TRUE(res.has_value());
134149
ASSERT_EQ(static_cast<int>(res.value()), 0);
135150
ASSERT_EQ(static_cast<int>(sc.getSourceIndex()), 5);
151+
ASSERT_TRUE(sc.isConversionComplete());
136152

137153
res = sc.popUTF8();
138154
ASSERT_FALSE(res.has_value());

0 commit comments

Comments
 (0)