Skip to content

Commit 28756ee

Browse files
committed
使用std::string 换掉stringstream
1 parent 99a9562 commit 28756ee

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

CodeService/src/FormatElement/RangeFormatContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void RangeFormatContext::Print(TextElement& textElement)
4949
}
5050
}
5151
}
52-
_os << textElement.GetText();
52+
_buffer.append(textElement.GetText());
5353
_characterCount += textElement.GetText().size();
5454

5555
if (startLine < _formattedRange.StartLine)
@@ -74,7 +74,7 @@ void RangeFormatContext::PrintBlank(int blank)
7474
{
7575
for (int i = 0; i < blank; i++)
7676
{
77-
_os << ' ';
77+
_buffer.push_back(' ');
7878
}
7979
}
8080
}
@@ -86,7 +86,7 @@ void RangeFormatContext::PrintLine(int line)
8686
{
8787
for (int i = 0; i < line; i++)
8888
{
89-
_os << _options.end_of_line;
89+
_buffer.append(_options.end_of_line);
9090
}
9191
}
9292
}

CodeService/src/FormatElement/SerializeContext.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void SerializeContext::Print(TextElement& textElement)
3232
}
3333
}
3434
}
35-
_os << textElement.GetText();
35+
_buffer.append(textElement.GetText());
3636
_characterCount += textElement.GetText().size();
3737
}
3838

@@ -44,7 +44,7 @@ void SerializeContext::PrintLine(int line)
4444
}
4545
for (int i = 0; i < line; i++)
4646
{
47-
_os << _options.end_of_line;
47+
_buffer.append(_options.end_of_line);
4848
_characterCount = 0;
4949
}
5050
}
@@ -57,14 +57,19 @@ void SerializeContext::PrintBlank(int blank)
5757
}
5858
for (int i = 0; i < blank; i++)
5959
{
60-
_os << ' ';
60+
_buffer.push_back(' ');
6161
_characterCount++;
6262
}
6363
}
6464

6565
std::string SerializeContext::GetText()
6666
{
67-
return _os.str();
67+
return std::move(_buffer);
68+
}
69+
70+
void SerializeContext::SetReadySize(std::size_t size)
71+
{
72+
_buffer.reserve(size);
6873
}
6974

7075
void SerializeContext::PrintIndent(std::size_t indent, IndentStyle style)
@@ -75,7 +80,7 @@ void SerializeContext::PrintIndent(std::size_t indent, IndentStyle style)
7580
{
7681
for (std::size_t i = 0; i < indent; i++)
7782
{
78-
_os << ' ';
83+
_buffer.push_back(' ');
7984
}
8085
_characterCount += indent;
8186
break;
@@ -84,7 +89,7 @@ void SerializeContext::PrintIndent(std::size_t indent, IndentStyle style)
8489
{
8590
for (std::size_t i = 0; i < indent; i++)
8691
{
87-
_os << '\t';
92+
_buffer.push_back('\t');
8893
}
8994
_characterCount += indent;
9095
break;

CodeService/src/LuaFormatter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void LuaFormatter::BuildFormattedElement()
6161
std::string LuaFormatter::GetFormattedText()
6262
{
6363
SerializeContext ctx(_parser, _options);
64+
ctx.SetReadySize(_parser->GetSource().size() + _parser->GetSource().size() / 2);
6465

6566
_env->Format(ctx);
6667

include/CodeService/FormatElement/SerializeContext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ class SerializeContext : public FormatContext
1616
virtual void PrintBlank(int blank);
1717

1818
virtual std::string GetText();
19+
20+
void SetReadySize(std::size_t size);
1921
protected:
2022
void PrintIndent(std::size_t indent, IndentStyle style);
21-
std::stringstream _os;
23+
std::string _buffer;
2224
};
2325

0 commit comments

Comments
 (0)