From 4b87549a645fd62107ddc5295a5027b2a5851096 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Mon, 10 Jan 2011 17:32:36 +0100 Subject: Added str_before and str_after functions str_before ("foo:bar:baz",':',"doesntmatter") returns "foo" str_after ("foo:bar:baz",':',"doesntmatter") returns "bar:baz" str_after and str_before ("foobar",':',"default") returns "default" Also, extract_var and extract_val now use the newly created functions --- synth/util.cpp | 22 ++++++++++++++++------ synth/util.h | 2 ++ 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'synth') diff --git a/synth/util.cpp b/synth/util.cpp index 51e6fda..2862fdb 100644 --- a/synth/util.cpp +++ b/synth/util.cpp @@ -175,24 +175,34 @@ parameter_enum param_to_enum(string param) return UNKNOWN; } -string extract_var(string s) +string str_before(string s, char delim, string onfail) { size_t p; - p=s.find('='); + p=s.find(delim); if (p!=string::npos) return s.substr(0,p); else - return ""; + return onfail; } -string extract_val(string s) +string str_after(string s, char delim, string onfail) { size_t p; - p=s.find('='); + p=s.find(delim); if (p!=string::npos) return s.substr(p+1); else - return s; + return onfail; +} + +string extract_var(string s) +{ + return str_before(s,'=',""); +} + +string extract_val(string s) +{ + return str_after(s,'=',s); } string fileext(string f) diff --git a/synth/util.h b/synth/util.h index e571c7d..ad88954 100644 --- a/synth/util.h +++ b/synth/util.h @@ -28,4 +28,6 @@ string extract_val(string s); string fileext(string f); +string str_before(string s, char delim, string onfail=""); +string str_after(string s, char delim, string onfail=""); #endif -- cgit v1.2.3