Commit 1d687403 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Less goto in ltrimWhitespace and rtrimWhitespace

Summary:
[Folly] Less `goto` in `ltrimWhitespace` and `rtrimWhitespace`.

The generated machine code remains unchanged for `g++ -O3` builds.

Generally, prefer to use `goto` in cases which cannot, or which can only arduously, be accomplished without it. In other words, reserve `goto` to cases where `goto` is necessary, rather than to cases where it saves one line.

Closes #732.

Reviewed By: andrewjcg

Differential Revision: D7027269

fbshipit-source-id: 91a4c5dba0e4c13dc727393799edc9b596cdb8b9
parent 69aa532b
...@@ -132,15 +132,17 @@ StringPiece ltrimWhitespace(StringPiece sp) { ...@@ -132,15 +132,17 @@ StringPiece ltrimWhitespace(StringPiece sp) {
// checked. This configuration where we loop on the ' ' // checked. This configuration where we loop on the ' '
// separately from oddspaces was empirically fastest. // separately from oddspaces was empirically fastest.
loop: while (true) {
for (; !sp.empty() && sp.front() == ' '; sp.pop_front()) { while (!sp.empty() && sp.front() == ' ') {
sp.pop_front();
} }
if (!sp.empty() && is_oddspace(sp.front())) { if (!sp.empty() && is_oddspace(sp.front())) {
sp.pop_front(); sp.pop_front();
goto loop; continue;
} }
return sp; return sp;
}
} }
StringPiece rtrimWhitespace(StringPiece sp) { StringPiece rtrimWhitespace(StringPiece sp) {
...@@ -148,15 +150,17 @@ StringPiece rtrimWhitespace(StringPiece sp) { ...@@ -148,15 +150,17 @@ StringPiece rtrimWhitespace(StringPiece sp) {
// checked. This configuration where we loop on the ' ' // checked. This configuration where we loop on the ' '
// separately from oddspaces was empirically fastest. // separately from oddspaces was empirically fastest.
loop: while (true) {
for (; !sp.empty() && sp.back() == ' '; sp.pop_back()) { while (!sp.empty() && sp.back() == ' ') {
sp.pop_back();
} }
if (!sp.empty() && is_oddspace(sp.back())) { if (!sp.empty() && is_oddspace(sp.back())) {
sp.pop_back(); sp.pop_back();
goto loop; continue;
} }
return sp; return sp;
}
} }
namespace { namespace {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment