From dcadd1f961342760a30babde0fb10c38a28b68d1 Mon Sep 17 00:00:00 2001 From: paulc Date: Mon, 28 Mar 2011 16:18:45 +0000 Subject: [PATCH] Added methods to extract a fragment of a String up to a separator. git-svn-id: http://voip.null.ro/svn/yate@4229 acf43c95-373e-0410-b603-e72c3f656dc1 --- engine/String.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ yateclass.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/engine/String.cpp b/engine/String.cpp index 846dd5fc..e03f8e0a 100644 --- a/engine/String.cpp +++ b/engine/String.cpp @@ -870,6 +870,52 @@ bool String::endsWith(const char* what, bool wordBreak, bool caseInsensitive) co return (::strncmp(m_string+m_length-l,what,l) == 0); } +String& String::extractTo(const char* sep, String& str) +{ + int pos = find(sep); + if (pos >= 0) { + str = substr(0,pos); + assign(m_string+pos+::strlen(sep)); + } + else { + str = *this; + clear(); + } + return *this; +} + +String& String::extractTo(const char* sep, bool& store) +{ + String str; + extractTo(sep,str); + store = str.toBoolean(store); + return *this; +} + +String& String::extractTo(const char* sep, int& store, int base) +{ + String str; + extractTo(sep,str); + store = str.toInteger(store,base); + return *this; +} + +String& String::extractTo(const char* sep, int& store, const TokenDict* tokens, int base) +{ + String str; + extractTo(sep,str); + store = str.toInteger(tokens,store,base); + return *this; +} + +String& String::extractTo(const char* sep, double& store) +{ + String str; + extractTo(sep,str); + store = str.toDouble(store); + return *this; +} + bool String::matches(const Regexp& rexp) { if (m_matches) diff --git a/yateclass.h b/yateclass.h index 82294a56..968e3ad4 100644 --- a/yateclass.h +++ b/yateclass.h @@ -1884,6 +1884,49 @@ public: */ bool startSkip(const char* what, bool wordBreak = true, bool caseInsensitive = false); + /** + * Extract a substring up to a separator + * @param sep Separator string to match after extracted fragment + * @param store Reference to String variable to store extracted fragment + * @return Reference to this string + */ + String& extractTo(const char* sep, String& store); + + /** + * Extract a boolean substring up to a separator + * @param sep Separator string to match after extracted fragment + * @param store Reference to boolean variable to store extracted fragment + * @return Reference to this string + */ + String& extractTo(const char* sep, bool& store); + + /** + * Extract an integer value substring up to a separator + * @param sep Separator string to match after extracted fragment + * @param store Reference to integer variable to store extracted fragment + * @param base Numeration base, 0 to autodetect + * @return Reference to this string + */ + String& extractTo(const char* sep, int& store, int base = 0); + + /** + * Extract an integer or token value substring up to a separator + * @param sep Separator string to match after extracted fragment + * @param store Reference to integer variable to store extracted fragment + * @param tokens Pointer to an array of tokens to lookup first + * @param base Numeration base, 0 to autodetect + * @return Reference to this string + */ + String& extractTo(const char* sep, int& store, const TokenDict* tokens, int base = 0); + + /** + * Extract a double value substring up to a separator + * @param sep Separator string to match after extracted fragment + * @param store Reference to double variable to store extracted fragment + * @return Reference to this string + */ + String& extractTo(const char* sep, double& store); + /** * Checks if matches another string * @param value String to check for match