1
1
Fork 0
forked from suyu/suyu

video_core/shader/ast: Make Indent() return a string_view

The returned string is simply a substring of our constexpr tabs
string_view, so we can just use a string_view here as well, since the
original string_view is guaranteed to always exist.

Now the function is fully non-allocating.
This commit is contained in:
Lioncash 2019-10-17 20:07:57 -04:00
parent 15d177a6ac
commit a2eccbf075

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <string> #include <string>
#include <string_view>
#include <fmt/format.h> #include <fmt/format.h>
@ -263,7 +264,9 @@ public:
} }
void operator()(const ASTIfElse& ast) { void operator()(const ASTIfElse& ast) {
inner += Indent() + "else {\n"; inner += Indent();
inner += "else {\n";
scope++; scope++;
ASTNode current = ast.nodes.GetFirst(); ASTNode current = ast.nodes.GetFirst();
while (current) { while (current) {
@ -271,15 +274,18 @@ public:
current = current->GetNext(); current = current->GetNext();
} }
scope--; scope--;
inner += Indent() + "}\n";
inner += Indent();
inner += "}\n";
} }
void operator()(const ASTBlockEncoded& ast) { void operator()(const ASTBlockEncoded& ast) {
inner += fmt::format("{}Block({}, {});\n", Indent(), ast.start, ast.end); inner += fmt::format("{}Block({}, {});\n", Indent(), ast.start, ast.end);
} }
void operator()(const ASTBlockDecoded& ast) { void operator()([[maybe_unused]] const ASTBlockDecoded& ast) {
inner += Indent() + "Block;\n"; inner += Indent();
inner += "Block;\n";
} }
void operator()(const ASTVarSet& ast) { void operator()(const ASTVarSet& ast) {
@ -335,22 +341,26 @@ public:
} }
private: private:
std::string& Indent() { std::string_view Indent() {
if (memo_scope == scope) { if (space_segment_scope == scope) {
return tabs_memo; return space_segment;
} }
tabs_memo = tabs.substr(0, scope * 2);
memo_scope = scope; // Ensure that we don't exceed our view.
return tabs_memo; ASSERT(scope * 2 < spaces.size());
space_segment = spaces.substr(0, scope * 2);
space_segment_scope = scope;
return space_segment;
} }
std::string inner{}; std::string inner{};
std::string_view space_segment;
u32 scope{}; u32 scope{};
u32 space_segment_scope{};
std::string tabs_memo{}; static constexpr std::string_view spaces{" "};
u32 memo_scope{};
static constexpr std::string_view tabs{" "};
}; };
std::string ASTManager::Print() { std::string ASTManager::Print() {