[nemiver/follow-fork-mode: 17/35] Detect when a string is a number
- From: Dodji Seketeli <dodji src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nemiver/follow-fork-mode: 17/35] Detect when a string is a number
- Date: Thu, 6 May 2010 10:17:31 +0000 (UTC)
commit 36125761ae9545a612c25a37218b6ca4d1f321e6
Author: Dodji Seketeli <dodji redhat com>
Date: Sat Apr 24 22:08:48 2010 +0200
Detect when a string is a number
* src/common/nmv-str-utils.[cc|] (string_is_number,
string_is_hexa_number): New fns.
src/common/nmv-str-utils.cc | 30 ++++++++++++++++++++++++++++++
src/common/nmv-str-utils.h | 2 ++
2 files changed, 32 insertions(+), 0 deletions(-)
---
diff --git a/src/common/nmv-str-utils.cc b/src/common/nmv-str-utils.cc
index f814b87..645090e 100644
--- a/src/common/nmv-str-utils.cc
+++ b/src/common/nmv-str-utils.cc
@@ -100,6 +100,36 @@ int_to_string (size_t an_int)
return str;
}
+bool
+string_is_number (const string &a_str)
+{
+ for (unsigned i = 0; i < a_str.size (); ++i)
+ if (!isdigit (a_str[i]))
+ return false;
+
+ return true;
+}
+
+bool
+string_is_hexa_number (const string &a_str)
+{
+
+ if (a_str.empty ())
+ return false;
+
+ unsigned i = 0;
+ if (a_str.size () > 2
+ && a_str[0] == '0'
+ && (a_str[1] == 'x' || a_str[1] == 'X'))
+ i = 2;
+
+ for (; i < a_str.size (); ++i)
+ if (!isxdigit (a_str[i]))
+ return false;
+
+ return true;
+}
+
std::vector<UString>
split (const UString &a_string, const UString &a_delim)
{
diff --git a/src/common/nmv-str-utils.h b/src/common/nmv-str-utils.h
index b901902..afed527 100644
--- a/src/common/nmv-str-utils.h
+++ b/src/common/nmv-str-utils.h
@@ -37,6 +37,8 @@ extract_path_and_line_num_from_location (const std::string &a_location,
std::string &a_line_num);
size_t hexa_to_int (const string &a_hexa_str);
std::string int_to_string (size_t an_int);
+bool string_is_number (const string&);
+bool string_is_hexa_number (const string &a_str);
vector<UString> split (const UString &a_string, const UString &a_delim);
vector<UString> split_set (const UString &a_string, const UString &a_delim_set);
UString join (const vector<UString> &a_elements,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]