summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--buildrun.cxx2
-rw-r--r--cache.cxx4
-rw-r--r--main.cxx11
-rw-r--r--parse.cxx8
-rw-r--r--tapset-mark.cxx2
-rw-r--r--tapset-utrace.cxx2
-rw-r--r--tapsets.cxx17
-rw-r--r--util.h20
8 files changed, 40 insertions, 26 deletions
diff --git a/buildrun.cxx b/buildrun.cxx
index 084022bf..0140c69f 100644
--- a/buildrun.cxx
+++ b/buildrun.cxx
@@ -629,7 +629,7 @@ make_typequery(systemtap_session& s, string& module)
int rc;
string new_module;
vector<string> headers;
- bool kernel = (module.compare(0, 6, "kernel") == 0);
+ bool kernel = startswith(module, "kernel");
for (size_t end, i = kernel ? 6 : 0; i < module.size(); i = end + 1)
{
diff --git a/cache.cxx b/cache.cxx
index 13436439..0334474b 100644
--- a/cache.cxx
+++ b/cache.cxx
@@ -81,7 +81,7 @@ add_script_to_cache(systemtap_session& s)
copy_file(module_src_path + ".sgn", s.hash_path + ".sgn", verbose);
string c_dest_path = s.hash_path;
- if (c_dest_path.rfind(".ko") == (c_dest_path.size() - 3))
+ if (endswith(c_dest_path, ".ko"))
c_dest_path.resize(c_dest_path.size() - 3);
c_dest_path += ".c";
@@ -140,7 +140,7 @@ get_script_from_cache(systemtap_session& s)
string c_src_path = s.hash_path;
int fd_module, fd_c;
- if (c_src_path.rfind(".ko") == (c_src_path.size() - 3))
+ if (endswith(c_src_path, ".ko"))
c_src_path.resize(c_src_path.size() - 3);
c_src_path += ".c";
diff --git a/main.cxx b/main.cxx
index 9cc0370d..93fd6bfe 100644
--- a/main.cxx
+++ b/main.cxx
@@ -436,7 +436,7 @@ int parse_kernel_config (systemtap_session &s)
string line;
while (getline (kcf, line))
{
- if (line.substr(0, 7) != "CONFIG_") continue;
+ if (!startswith(line, "CONFIG_")) continue;
size_t off = line.find('=');
if (off == string::npos) continue;
string key = line.substr(0, off);
@@ -750,20 +750,17 @@ main (int argc, char * const argv [])
save_module = true;
// XXX: convert to assert_regexp_match()
{
- string::size_type len = s.module_name.length();
-
// If the module name ends with '.ko', chop it off since
// modutils doesn't like modules named 'foo.ko.ko'.
- if (len > 3 && s.module_name.substr(len - 3, 3) == ".ko")
+ if (endswith(s.module_name, ".ko"))
{
- s.module_name.erase(len - 3);
- len -= 3;
+ s.module_name.erase(s.module_name.size() - 3);
cerr << "Truncating module name to '" << s.module_name
<< "'" << endl;
}
// Make sure an empty module name wasn't specified (-m "")
- if (len == 0)
+ if (s.module_name.empty())
{
cerr << "Module name cannot be empty." << endl;
exit(1);
diff --git a/parse.cxx b/parse.cxx
index c517cd1a..06ba88e9 100644
--- a/parse.cxx
+++ b/parse.cxx
@@ -274,7 +274,7 @@ bool eval_pp_conditional (systemtap_session& s,
return result;
}
- else if (l->type == tok_identifier && l->content.substr(0,7) == "CONFIG_")
+ else if (l->type == tok_identifier && startswith(l->content, "CONFIG_"))
{
if (r->type == tok_string)
{
@@ -306,7 +306,7 @@ bool eval_pp_conditional (systemtap_session& s,
return eval_comparison (lhs, op, rhs);
}
else if (r->type == tok_identifier
- && r->content.substr(0,7) == "CONFIG_")
+ && startswith(r->content, "CONFIG_"))
{
// First try to convert both to numbers,
// otherwise threat both as strings.
@@ -2679,9 +2679,9 @@ target_symbol* parser::parse_target_symbol (const token* t)
expect_unknown(tok_string, cop->type);
// types never start with "struct<space>" or "union<space>",
// so gobble it up.
- if (cop->type.compare(0, 7, "struct ") == 0)
+ if (startswith(cop->type, "struct "))
cop->type = cop->type.substr(7);
- if (cop->type.compare(0, 6, "union ") == 0)
+ if (startswith(cop->type, "union "))
cop->type = cop->type.substr(6);
if (peek_op (","))
{
diff --git a/tapset-mark.cxx b/tapset-mark.cxx
index 5c5fb0ce..c672dc7a 100644
--- a/tapset-mark.cxx
+++ b/tapset-mark.cxx
@@ -187,7 +187,7 @@ mark_var_expanding_visitor::visit_target_symbol (target_symbol* e)
if (e->addressof)
throw semantic_error("cannot take address of marker variable", e->tok);
- if (e->base_name.substr(0,4) == "$arg")
+ if (startswith(e->base_name, "$arg"))
visit_target_symbol_arg (e);
else if (e->base_name == "$format" || e->base_name == "$name"
|| e->base_name == "$$parms" || e->base_name == "$$vars")
diff --git a/tapset-utrace.cxx b/tapset-utrace.cxx
index 64433a24..25e0b15e 100644
--- a/tapset-utrace.cxx
+++ b/tapset-utrace.cxx
@@ -591,7 +591,7 @@ utrace_var_expanding_visitor::visit_target_symbol (target_symbol* e)
if (e->addressof)
throw semantic_error("cannot take address of utrace variable", e->tok);
- if (e->base_name.substr(0,4) == "$arg" || e->base_name == "$$parms")
+ if (startswith(e->base_name, "$arg") || e->base_name == "$$parms")
visit_target_symbol_arg(e);
else if (e->base_name == "$syscall" || e->base_name == "$return")
visit_target_symbol_context(e);
diff --git a/tapsets.cxx b/tapsets.cxx
index 2068d5f4..b572f4e0 100644
--- a/tapsets.cxx
+++ b/tapsets.cxx
@@ -2766,7 +2766,7 @@ void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
// look for "<path/to/header>" or "kernel<path/to/header>"
// for those cases, build a module including that header
if (module[module.size() - 1] == '>' &&
- (module[0] == '<' || module.compare(0, 7, "kernel<") == 0))
+ (module[0] == '<' || startswith(module, "kernel<")))
{
string cached_module;
if (s.use_cache)
@@ -3768,7 +3768,7 @@ sdt_var_expanding_visitor::visit_target_symbol (target_symbol *e)
return;
}
- if (e->base_name.find("$arg") == string::npos || ! have_reg_args)
+ if (!startswith(e->base_name, "$arg") || ! have_reg_args)
{
// NB: uprobes-based sdt.h; $argFOO gets resolved later.
// XXX: We don't even know the arg_count in this case.
@@ -6463,7 +6463,7 @@ tracepoint_query::handle_query_func(Dwarf_Die * func)
{
dw.focus_on_function (func);
- assert(dw.function_name.compare(0, 10, "stapprobe_") == 0);
+ assert(startswith(dw.function_name, "stapprobe_"));
string tracepoint_instance = dw.function_name.substr(10);
// check for duplicates -- sometimes tracepoint headers may be indirectly
@@ -6609,13 +6609,10 @@ tracepoint_builder::init_dw(systemtap_session& s)
string header(trace_glob.gl_pathv[i]);
// filter out a few known "internal-only" headers
- if (header.find("/define_trace.h") != string::npos)
- continue;
- if (header.find("/ftrace.h") != string::npos)
- continue;
- if (header.find("/trace_events.h") != string::npos)
- continue;
- if (header.find("_event_types.h") != string::npos)
+ if (endswith(header, "/define_trace.h") ||
+ endswith(header, "/ftrace.h") ||
+ endswith(header, "/trace_events.h") ||
+ endswith(header, "_event_types.h"))
continue;
system_headers.push_back(header);
diff --git a/util.h b/util.h
index 75e198ca..c6cb4554 100644
--- a/util.h
+++ b/util.h
@@ -1,3 +1,4 @@
+#include <cstring>
#include <string>
#include <vector>
#include <iostream>
@@ -112,4 +113,23 @@ void delete_map(T& t)
}
+// Returns whether a string starts with the given prefix
+inline bool
+startswith(const std::string & s, const char * prefix)
+{
+ return (s.compare(0, std::strlen(prefix), prefix) == 0);
+}
+
+
+// Returns whether a string ends with the given suffix
+inline bool
+endswith(const std::string & s, const char * suffix)
+{
+ size_t s_len = s.size(), suffix_len = std::strlen(suffix);
+ if (suffix_len > s_len)
+ return false;
+ return (s.compare(s_len - suffix_len, suffix_len, suffix) == 0);
+}
+
+
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */