Skip to content

Commit 9a5f24e

Browse files
committed
performance: Implement getUntilAscii for Pig
1 parent d669034 commit 9a5f24e

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/Pig.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@ bool Pig::skipPartial (const std::string& reference, std::string& result,
124124
return false;
125125
}
126126

127+
////////////////////////////////////////////////////////////////////////////////
128+
// ASCII-only version of getUntil. Has better performance characteristics at
129+
// the cost of not considering utf-8 characters correctly. Use with caution.
130+
bool Pig::getUntilAscii (char end, std::string& result)
131+
{
132+
auto save = _cursor;
133+
auto found = _text->find (end, _cursor + 1);
134+
135+
if (found == std::string::npos)
136+
{
137+
found = _text->size ();
138+
result = _text->substr (_cursor, found - _cursor);
139+
_cursor = found;
140+
return true;
141+
}
142+
143+
result = _text->substr (_cursor, found - _cursor);
144+
_cursor = _cursor + result.size();
145+
146+
return _cursor > save;
147+
}
148+
127149
////////////////////////////////////////////////////////////////////////////////
128150
bool Pig::getUntil (int end, std::string& result)
129151
{

src/Pig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Pig
4343
bool skipPartial (const std::string&, std::string&, bool ignore_case = false);
4444

4545
bool getUntil (int, std::string&);
46+
bool getUntilAscii (char, std::string&);
4647
bool getUntilWS (std::string&);
4748
bool getCharacter (int&);
4849
bool getDigit (int&);

0 commit comments

Comments
 (0)