Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/auxiliary')
-rw-r--r--src/auxiliary/functions.cc22
-rw-r--r--src/auxiliary/functions.h6
2 files changed, 28 insertions, 0 deletions
diff --git a/src/auxiliary/functions.cc b/src/auxiliary/functions.cc
index dbfc799..a6187af 100644
--- a/src/auxiliary/functions.cc
+++ b/src/auxiliary/functions.cc
@@ -143,4 +143,26 @@ void trim(std::string &text)
}
}
+void to_label(std::string &text)
+{
+ trim(text);
+ size_t pos = 0;
+
+ while (pos < text.size()) {
+ if ((text[pos] == ' ') || (text[pos] == '-')) {
+ text[pos] = '_';
+ pos++;
+ } else if ((text[pos] >= 'A') && (text[pos] <= 'Z')) {
+ text[pos] = text[pos] + 'a' - 'A';
+ pos++;
+ } else if ((text[pos] >= 'a') && (text[pos] <= 'z')) {
+ pos++;
+ } else if ((text[pos] >= '0') && (text[pos] <= '9')) {
+ pos++;
+ } else {
+ text.erase(pos, 1);
+ }
+ }
+}
+
}
diff --git a/src/auxiliary/functions.h b/src/auxiliary/functions.h
index 557ab45..65b6a2b 100644
--- a/src/auxiliary/functions.h
+++ b/src/auxiliary/functions.h
@@ -55,6 +55,12 @@ const std::string text_strip_lowercase(const std::string &text);
/// trim leading ad trailing spaces from a string
void trim(std::string &text);
+/// convert a string to a valid label string
+/** trim leading and trealing spaces, convert remaining spaces to underscores, and make lowercase
+ * remove any non-alphanumeric character
+ */
+void to_label(std::string &text);
+
}
#endif // __INCLUDED_AUX_FUNCTIONS_H__