aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Neto <dneto@google.com>2018-10-16 09:56:40 -0400
committerDavid Neto <dneto@google.com>2018-10-16 10:01:00 -0400
commitb67f62580ea6948df2da631852cc2d51985c6d57 (patch)
tree318eefe42b72f70ab186d5d80a72bc5150120440
parent758aca7f495a855ba761302bd830056d15019a03 (diff)
downloadeffcee-b67f62580ea6948df2da631852cc2d51985c6d57.tar.gz
Avoid StringPiece::as_string
Use an inlined helper function instead. Also clang-format affected files. Fixes #23
-rw-r--r--effcee/check.cc24
-rw-r--r--effcee/match.cc29
-rw-r--r--effcee/to_string.h29
3 files changed, 56 insertions, 26 deletions
diff --git a/effcee/check.cc b/effcee/check.cc
index 612f7f1..2751801 100644
--- a/effcee/check.cc
+++ b/effcee/check.cc
@@ -14,8 +14,8 @@
#include "check.h"
-#include <cassert>
#include <algorithm>
+#include <cassert>
#include <memory>
#include <sstream>
#include <string>
@@ -24,6 +24,7 @@
#include "cursor.h"
#include "effcee.h"
#include "make_unique.h"
+#include "to_string.h"
using Status = effcee::Result::Status;
using StringPiece = effcee::StringPiece;
@@ -67,7 +68,7 @@ Check::Check(Type type, StringPiece param) : type_(type), param_(param) {
bool Check::Part::MightMatch(const VarMapping& vars) const {
return type_ != Type::VarUse ||
- vars.find(VarUseName().as_string()) != vars.end();
+ vars.find(ToString(VarUseName())) != vars.end();
}
std::string Check::Part::Regex(const VarMapping& vars) const {
@@ -75,11 +76,11 @@ std::string Check::Part::Regex(const VarMapping& vars) const {
case Type::Fixed:
return RE2::QuoteMeta(param_);
case Type::Regex:
- return param_.as_string();
+ return ToString(param_);
case Type::VarDef:
- return std::string("(") + expression_.as_string() + ")";
+ return std::string("(") + ToString(expression_) + ")";
case Type::VarUse: {
- auto where = vars.find(VarUseName().as_string());
+ auto where = vars.find(ToString(VarUseName()));
if (where != vars.end()) {
// Return the escaped form of the current value of the variable.
return RE2::QuoteMeta((*where).second);
@@ -89,7 +90,7 @@ std::string Check::Part::Regex(const VarMapping& vars) const {
}
}
}
- return ""; // Unreachable. But we need to satisfy GCC.
+ return ""; // Unreachable. But we need to satisfy GCC.
}
bool Check::Matches(StringPiece* input, StringPiece* captured,
@@ -107,7 +108,7 @@ bool Check::Matches(StringPiece* input, StringPiece* captured,
consume_regex << part->Regex(*vars);
const auto var_def_name = part->VarDefName();
if (!var_def_name.empty()) {
- var_def_indices[num_captures++] = var_def_name.as_string();
+ var_def_indices[num_captures++] = ToString(var_def_name);
}
num_captures += part->NumCapturingGroups();
}
@@ -121,7 +122,7 @@ bool Check::Matches(StringPiece* input, StringPiece* captured,
// Update the variable mapping.
for (auto& var_def_index : var_def_indices) {
const int index = var_def_index.first;
- (*vars)[var_def_index.second] = captures[index].as_string();
+ (*vars)[var_def_index.second] = ToString(captures[index]);
}
}
@@ -129,8 +130,9 @@ bool Check::Matches(StringPiece* input, StringPiece* captured,
}
namespace {
-// Returns a parts list for the given pattern. This splits out regular expressions as
-// delimited by {{ and }}, and also variable uses and definitions.
+// Returns a parts list for the given pattern. This splits out regular
+// expressions as delimited by {{ and }}, and also variable uses and
+// definitions.
Check::Parts PartsForPattern(StringPiece pattern) {
Check::Parts parts;
StringPiece fixed, regex, var;
@@ -192,7 +194,7 @@ Check::Parts PartsForPattern(StringPiece pattern) {
return parts;
}
-} // anonymous
+} // namespace
std::pair<Result, CheckList> ParseChecks(StringPiece str,
const Options& options) {
diff --git a/effcee/match.cc b/effcee/match.cc
index bd4a224..cb9517f 100644
--- a/effcee/match.cc
+++ b/effcee/match.cc
@@ -12,17 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include <cassert>
#include <algorithm>
-#include <string>
+#include <cassert>
#include <sstream>
+#include <string>
#include <vector>
#include "check.h"
#include "cursor.h"
#include "diagnostic.h"
#include "effcee.h"
-
+#include "to_string.h"
using effcee::Check;
using Status = effcee::Result::Status;
@@ -95,9 +95,10 @@ Result Match(StringPiece input, StringPiece checks, const Options& options) {
const auto var_use = part->VarUseName();
if (!var_use.empty()) {
std::ostringstream phrase;
- if (vars.find(var_use.as_string()) != vars.end()) {
+ std::string var_use_str(ToString(var_use));
+ if (vars.find(var_use_str) != vars.end()) {
phrase << "note: with variable \"" << var_use << "\" equal to \""
- << vars[var_use.as_string()] << "\"";
+ << vars[var_use_str] << "\"";
} else {
phrase << "note: uses undefined variable \"" << var_use << "\"";
}
@@ -111,8 +112,8 @@ Result Match(StringPiece input, StringPiece checks, const Options& options) {
for (; !cursor.Exhausted(); cursor.AdvanceLine()) {
// Try to match the current line against the unresolved checks.
- // The number of characters the cursor should advance to accommodate a recent
- // DAG check match.
+ // The number of characters the cursor should advance to accommodate a
+ // recent DAG check match.
size_t deferred_advance = 0;
bool scan_this_line = true;
@@ -196,14 +197,12 @@ Result Match(StringPiece input, StringPiece checks, const Options& options) {
}
if (check.type() != Type::DAG && first_unresolved_dag < i) {
- return fail() << check_msg(
- pattern[first_unresolved_dag].param(),
- "error: expected string not found in input")
- << input_msg(previous_match_end,
- "note: scanning from here")
- << input_msg(captured,
- "note: next check matches here")
- << var_notes(previous_match_end, check);
+ return fail()
+ << check_msg(pattern[first_unresolved_dag].param(),
+ "error: expected string not found in input")
+ << input_msg(previous_match_end, "note: scanning from here")
+ << input_msg(captured, "note: next check matches here")
+ << var_notes(previous_match_end, check);
}
resolved[i] = true;
diff --git a/effcee/to_string.h b/effcee/to_string.h
new file mode 100644
index 0000000..b2ccc4f
--- /dev/null
+++ b/effcee/to_string.h
@@ -0,0 +1,29 @@
+// Copyright 2018 The Effcee Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef EFFCEE_TO_STRING_H
+#define EFFCEE_TO_STRING_H
+
+#include <string>
+#include "effcee.h"
+
+namespace effcee {
+
+// Returns a copy of a StringPiece, as a std::string.
+inline std::string ToString(effcee::StringPiece s) {
+ return std::string(s.data(), s.size());
+}
+} // namespace effcee
+
+#endif