Skip to content

Commit 4752226

Browse files
committed
Use a string_view for ast strings as much as possible
1 parent 6c0ac9e commit 4752226

File tree

6 files changed

+62
-37
lines changed

6 files changed

+62
-37
lines changed

include/Validation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ struct ValidateArgumentMap
7979
};
8080

8181
using ValidateArgumentVariant = std::variant<ValidateArgumentVariable, response::IntType,
82-
response::FloatType, response::StringType, response::BooleanType, ValidateArgumentEnumValue,
82+
response::FloatType, std::string_view, response::BooleanType, ValidateArgumentEnumValue,
8383
ValidateArgumentList, ValidateArgumentMap>;
8484

8585
struct ValidateArgumentValue
8686
{
8787
ValidateArgumentValue(ValidateArgumentVariable&& value);
8888
ValidateArgumentValue(response::IntType value);
8989
ValidateArgumentValue(response::FloatType value);
90-
ValidateArgumentValue(response::StringType&& value);
90+
ValidateArgumentValue(std::string_view value);
9191
ValidateArgumentValue(response::BooleanType value);
9292
ValidateArgumentValue(ValidateArgumentEnumValue&& value);
9393
ValidateArgumentValue(ValidateArgumentList&& value);

include/graphqlservice/GraphQLTree.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <tao/pegtl/contrib/parse_tree.hpp>
1313

1414
#include <string>
15+
#include <string_view>
1516
#include <variant>
1617
#include <vector>
1718

@@ -21,7 +22,9 @@ using namespace tao::graphqlpeg;
2122

2223
struct ast_node : parse_tree::basic_node<ast_node>
2324
{
24-
std::string unescaped;
25+
std::string_view unescaped_view() const;
26+
27+
std::variant<std::string_view, std::string> unescaped;
2528
};
2629

2730
struct ast_input

src/GraphQLService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void ValueVisitor::visitFloatValue(const peg::ast_node& floatValue)
253253

254254
void ValueVisitor::visitStringValue(const peg::ast_node& stringValue)
255255
{
256-
_value = response::Value(std::string(stringValue.unescaped));
256+
_value = response::Value(std::string { stringValue.unescaped_view() });
257257
}
258258

259259
void ValueVisitor::visitBooleanValue(const peg::ast_node& booleanValue)

src/GraphQLTree.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@
1212
#include <numeric>
1313
#include <tuple>
1414

15+
using namespace std::literals;
16+
1517
namespace graphql {
1618
namespace peg {
1719

20+
std::string_view ast_node::unescaped_view() const
21+
{
22+
return std::visit(
23+
[](const auto& value) noexcept {
24+
return std::string_view { value };
25+
},
26+
unescaped);
27+
}
28+
1829
using namespace tao::graphqlpeg;
1930

2031
template <typename Rule>
@@ -69,12 +80,18 @@ struct ast_selector<escaped_unicode> : std::true_type
6980
{
7081
if (n->has_content())
7182
{
83+
auto unescaped = std::visit(
84+
[](auto&& unescaped) noexcept {
85+
return std::string { std::move(unescaped) };
86+
},
87+
std::move(n->unescaped));
7288
auto content = n->string_view();
7389

74-
if (unescape::utf8_append_utf32(n->unescaped,
90+
if (unescape::utf8_append_utf32(unescaped,
7591
unescape::unhex_string<uint32_t>(content.data() + 1,
7692
content.data() + content.size())))
7793
{
94+
n->unescaped = std::move(unescaped);
7895
return;
7996
}
8097
}
@@ -95,35 +112,35 @@ struct ast_selector<escaped_char> : std::true_type
95112
switch (ch)
96113
{
97114
case '"':
98-
n->unescaped = "\"";
115+
n->unescaped = "\""sv;
99116
return;
100117

101118
case '\\':
102-
n->unescaped = "\\";
119+
n->unescaped = "\\"sv;
103120
return;
104121

105122
case '/':
106-
n->unescaped = "/";
123+
n->unescaped = "/"sv;
107124
return;
108125

109126
case 'b':
110-
n->unescaped = "\b";
127+
n->unescaped = "\b"sv;
111128
return;
112129

113130
case 'f':
114-
n->unescaped = "\f";
131+
n->unescaped = "\f"sv;
115132
return;
116133

117134
case 'n':
118-
n->unescaped = "\n";
135+
n->unescaped = "\n"sv;
119136
return;
120137

121138
case 'r':
122-
n->unescaped = "\r";
139+
n->unescaped = "\r"sv;
123140
return;
124141

125142
case 't':
126-
n->unescaped = "\t";
143+
n->unescaped = "\t"sv;
127144
return;
128145

129146
default:
@@ -149,7 +166,7 @@ struct ast_selector<block_escape_sequence> : std::true_type
149166
{
150167
static void transform(std::unique_ptr<ast_node>& n)
151168
{
152-
n->unescaped = R"bq(""")bq";
169+
n->unescaped = R"bq(""")bq"sv;
153170
}
154171
};
155172

@@ -171,17 +188,21 @@ struct ast_selector<string_value> : std::true_type
171188
{
172189
if (n->children.size() > 1)
173190
{
174-
n->unescaped.reserve(std::accumulate(n->children.cbegin(),
191+
std::string unescaped;
192+
193+
unescaped.reserve(std::accumulate(n->children.cbegin(),
175194
n->children.cend(),
176195
size_t(0),
177196
[](size_t total, const std::unique_ptr<ast_node>& child) {
178-
return total + child->unescaped.size();
197+
return total + child->unescaped_view().size();
179198
}));
180199

181200
for (const auto& child : n->children)
182201
{
183-
n->unescaped.append(child->unescaped);
202+
unescaped.append(child->unescaped_view());
184203
}
204+
205+
n->unescaped = std::move(unescaped);
185206
}
186207
else
187208
{

src/SchemaGenerator.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ void Generator::visitObjectTypeDefinition(const peg::ast_node& objectTypeDefinit
812812
[&description](const peg::ast_node& child) {
813813
if (!child.children.empty())
814814
{
815-
description = child.children.front()->unescaped;
815+
description = child.children.front()->unescaped_view();
816816
}
817817
});
818818

@@ -874,7 +874,7 @@ void Generator::visitInterfaceTypeDefinition(const peg::ast_node& interfaceTypeD
874874
[&description](const peg::ast_node& child) {
875875
if (!child.children.empty())
876876
{
877-
description = child.children.front()->unescaped;
877+
description = child.children.front()->unescaped_view();
878878
}
879879
});
880880

@@ -931,7 +931,7 @@ void Generator::visitInputObjectTypeDefinition(const peg::ast_node& inputObjectT
931931
[&description](const peg::ast_node& child) {
932932
if (!child.children.empty())
933933
{
934-
description = child.children.front()->unescaped;
934+
description = child.children.front()->unescaped_view();
935935
}
936936
});
937937

@@ -987,7 +987,7 @@ void Generator::visitEnumTypeDefinition(const peg::ast_node& enumTypeDefinition)
987987
[&description](const peg::ast_node& child) {
988988
if (!child.children.empty())
989989
{
990-
description = child.children.front()->unescaped;
990+
description = child.children.front()->unescaped_view();
991991
}
992992
});
993993

@@ -1029,7 +1029,7 @@ void Generator::visitEnumTypeExtension(const peg::ast_node& enumTypeExtension)
10291029
[&value](const peg::ast_node& description) {
10301030
if (!description.children.empty())
10311031
{
1032-
value.description = description.children.front()->unescaped;
1032+
value.description = description.children.front()->unescaped_view();
10331033
}
10341034
});
10351035

@@ -1063,7 +1063,7 @@ void Generator::visitEnumTypeExtension(const peg::ast_node& enumTypeExtension)
10631063
peg::on_first_child<peg::string_value>(argument,
10641064
[&value](const peg::ast_node& argumentValue) {
10651065
value.deprecationReason =
1066-
argumentValue.unescaped;
1066+
argumentValue.unescaped_view();
10671067
});
10681068
}
10691069
});
@@ -1092,7 +1092,7 @@ void Generator::visitScalarTypeDefinition(const peg::ast_node& scalarTypeDefinit
10921092
[&description](const peg::ast_node& child) {
10931093
if (!child.children.empty())
10941094
{
1095-
description = child.children.front()->unescaped;
1095+
description = child.children.front()->unescaped_view();
10961096
}
10971097
});
10981098

@@ -1115,7 +1115,7 @@ void Generator::visitUnionTypeDefinition(const peg::ast_node& unionTypeDefinitio
11151115
[&description](const peg::ast_node& child) {
11161116
if (!child.children.empty())
11171117
{
1118-
description = child.children.front()->unescaped;
1118+
description = child.children.front()->unescaped_view();
11191119
}
11201120
});
11211121

@@ -1164,7 +1164,7 @@ void Generator::visitDirectiveDefinition(const peg::ast_node& directiveDefinitio
11641164
[&directive](const peg::ast_node& child) {
11651165
if (!child.children.empty())
11661166
{
1167-
directive.description = child.children.front()->unescaped;
1167+
directive.description = child.children.front()->unescaped_view();
11681168
}
11691169
});
11701170

@@ -1242,7 +1242,7 @@ OutputFieldList Generator::getOutputFields(
12421242
}
12431243
else if (child->is_type<peg::description>() && !child->children.empty())
12441244
{
1245-
field.description = child->children.front()->unescaped;
1245+
field.description = child->children.front()->unescaped_view();
12461246
}
12471247
else if (child->is_type<peg::directives>())
12481248
{
@@ -1275,7 +1275,7 @@ OutputFieldList Generator::getOutputFields(
12751275
peg::on_first_child<peg::string_value>(argument,
12761276
[&deprecationReason](
12771277
const peg::ast_node& reason) {
1278-
deprecationReason = reason.unescaped;
1278+
deprecationReason = reason.unescaped_view();
12791279
});
12801280
}
12811281
});
@@ -1330,7 +1330,7 @@ InputFieldList Generator::getInputFields(const std::vector<std::unique_ptr<peg::
13301330
}
13311331
else if (child->is_type<peg::description>() && !child->children.empty())
13321332
{
1333-
field.description = child->children.front()->unescaped;
1333+
field.description = child->children.front()->unescaped_view();
13341334
}
13351335
}
13361336

@@ -1455,7 +1455,7 @@ void Generator::DefaultValueVisitor::visitFloatValue(const peg::ast_node& floatV
14551455

14561456
void Generator::DefaultValueVisitor::visitStringValue(const peg::ast_node& stringValue)
14571457
{
1458-
_value = response::Value(std::string(stringValue.unescaped));
1458+
_value = response::Value(std::string { stringValue.unescaped_view() });
14591459
}
14601460

14611461
void Generator::DefaultValueVisitor::visitBooleanValue(const peg::ast_node& booleanValue)
@@ -2593,7 +2593,8 @@ Operations::Operations()cpp";
25932593
{
25942594
bool firstValue = true;
25952595

2596-
sourceFile << R"cpp( type)cpp" << unionType.cppType << R"cpp(->AddPossibleTypes({
2596+
sourceFile << R"cpp( type)cpp" << unionType.cppType
2597+
<< R"cpp(->AddPossibleTypes({
25972598
)cpp";
25982599

25992600
for (const auto& unionOption : unionType.options)

src/Validation.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ValidateArgumentValue::ValidateArgumentValue(response::FloatType value)
7171
{
7272
}
7373

74-
ValidateArgumentValue::ValidateArgumentValue(response::StringType&& value)
74+
ValidateArgumentValue::ValidateArgumentValue(std::string_view value)
7575
: data(std::move(value))
7676
{
7777
}
@@ -177,10 +177,10 @@ void ValidateArgumentValueVisitor::visitFloatValue(const peg::ast_node& floatVal
177177

178178
void ValidateArgumentValueVisitor::visitStringValue(const peg::ast_node& stringValue)
179179
{
180-
response::StringType value { stringValue.unescaped };
180+
std::string_view value { stringValue.unescaped_view() };
181181
auto position = stringValue.begin();
182182

183-
_argumentValue.value = std::make_unique<ValidateArgumentValue>(std::move(value));
183+
_argumentValue.value = std::make_unique<ValidateArgumentValue>(value);
184184
_argumentValue.position = { position.line, position.column };
185185
}
186186

@@ -1161,19 +1161,19 @@ bool ValidateExecutableVisitor::validateInputValue(
11611161
}
11621162
else if (name == R"gql(String)gql"sv)
11631163
{
1164-
if (!std::holds_alternative<response::StringType>(argument.value->data))
1164+
if (!std::holds_alternative<std::string_view>(argument.value->data))
11651165
{
11661166
_errors.push_back({ "Expected String value", argument.position });
11671167
return false;
11681168
}
11691169
}
11701170
else if (name == R"gql(ID)gql"sv)
11711171
{
1172-
if (std::holds_alternative<response::StringType>(argument.value->data))
1172+
if (std::holds_alternative<std::string_view>(argument.value->data))
11731173
{
11741174
try
11751175
{
1176-
const auto& value = std::get<response::StringType>(argument.value->data);
1176+
const auto value = std::get<std::string_view>(argument.value->data);
11771177
auto decoded = Base64::fromBase64(value.data(), value.size());
11781178

11791179
return true;

0 commit comments

Comments
 (0)