Skip to content

Commit ac822ad

Browse files
committed
Preserve binary literals in Wave output
1 parent 38e7b5b commit ac822ad

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

src/nbl/asset/utils/CWaveStringResolver.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,48 @@ std::string tokenValueToString(const TokenT& token)
134134
return std::string(value.data(), value.size());
135135
}
136136

137+
bool isBinaryLiteralTail(const std::string_view value)
138+
{
139+
if (value.size() < 2u)
140+
return false;
141+
142+
if (value.front() != 'b' && value.front() != 'B')
143+
return false;
144+
145+
bool hasBinaryDigit = false;
146+
size_t i = 1u;
147+
for (; i < value.size(); ++i)
148+
{
149+
const char c = value[i];
150+
if (c == '0' || c == '1')
151+
{
152+
hasBinaryDigit = true;
153+
continue;
154+
}
155+
if (c == '\'')
156+
continue;
157+
break;
158+
}
159+
160+
if (!hasBinaryDigit)
161+
return false;
162+
163+
for (; i < value.size(); ++i)
164+
{
165+
const char c = value[i];
166+
const bool isIntegerSuffix = c == 'u' || c == 'U' || c == 'l' || c == 'L' || c == 'z' || c == 'Z';
167+
if (!isIntegerSuffix)
168+
return false;
169+
}
170+
171+
return true;
172+
}
173+
174+
bool shouldConcatenateWithoutWhitespace(const std::string_view previousValue, const std::string_view currentValue)
175+
{
176+
return previousValue == "0" && isBinaryLiteralTail(currentValue);
177+
}
178+
137179
core::string renderPreprocessedOutput(nbl::wave::context& context)
138180
{
139181
using namespace boost::wave;
@@ -142,6 +184,7 @@ core::string renderPreprocessedOutput(nbl::wave::context& context)
142184
util::insert_whitespace_detection whitespace(true);
143185
std::optional<nbl::wave::context::position_type> previousPosition = std::nullopt;
144186
bool previousWasExplicitWhitespace = false;
187+
std::string previousTokenValue;
145188

146189
for (auto it = context.begin(); it != context.end(); ++it)
147190
{
@@ -168,7 +211,7 @@ core::string renderPreprocessedOutput(nbl::wave::context& context)
168211
whitespace.shift_tokens(T_NEWLINE);
169212
}
170213
}
171-
else if (!previousWasExplicitWhitespace && whitespace.must_insert(id, value))
214+
else if (!previousWasExplicitWhitespace && !shouldConcatenateWithoutWhitespace(previousTokenValue, value) && whitespace.must_insert(id, value))
172215
{
173216
if (output.empty() || (output.back() != ' ' && output.back() != '\n' && output.back() != '\r' && output.back() != '\t'))
174217
{
@@ -182,6 +225,8 @@ core::string renderPreprocessedOutput(nbl::wave::context& context)
182225
whitespace.shift_tokens(id);
183226
previousPosition = position;
184227
previousWasExplicitWhitespace = explicitWhitespace;
228+
if (!explicitWhitespace)
229+
previousTokenValue = value;
185230
}
186231

187232
return output;

0 commit comments

Comments
 (0)