summaryrefslogtreecommitdiff
path: root/sbuild
diff options
context:
space:
mode:
authorRoger Leigh <rleigh@debian.org>2009-07-05 17:22:48 +0100
committerRoger Leigh <rleigh@debian.org>2009-07-05 17:22:48 +0100
commit43cfe200c2f00f36476c704739c061c993c40b8f (patch)
treeb6c5caf2ddc3a7b6a58b0211b15c4b213dd3b015 /sbuild
parent4801d8ac4c3975a6be488ee0577f1a36f3945acf (diff)
downloadschroot-43cfe200c2f00f36476c704739c061c993c40b8f.tar.gz
[sbuild::util] Add split_string_strict
split_string splits a string into chunks separated by the specified separator. split_string_strict doesn't eliminate empty strings when the separator appears at the string start or end, or if multiple separators are joined together.
Diffstat (limited to 'sbuild')
-rw-r--r--sbuild/sbuild-util.cc36
-rw-r--r--sbuild/sbuild-util.h54
2 files changed, 88 insertions, 2 deletions
diff --git a/sbuild/sbuild-util.cc b/sbuild/sbuild-util.cc
index b9403aa0..dc9e7a23 100644
--- a/sbuild/sbuild-util.cc
+++ b/sbuild/sbuild-util.cc
@@ -235,7 +235,13 @@ sbuild::split_string (std::string const& value,
while (pos !=std::string::npos || last_pos != std::string::npos)
{
// Add to list
- ret.push_back(value.substr(last_pos, pos - last_pos));
+ if (pos == std::string::npos)
+ // Entire string from last_pos
+ ret.push_back(value.substr(last_pos, pos));
+ else
+ // Between pos and last_pos
+ ret.push_back(value.substr(last_pos, pos - last_pos));
+
// Find next
last_pos = value.find_first_not_of(separator, pos);
pos = value.find_first_of(separator, last_pos);
@@ -244,6 +250,34 @@ sbuild::split_string (std::string const& value,
return ret;
}
+string_list
+sbuild::split_string_strict (std::string const& value,
+ std::string const& separator)
+{
+ string_list ret;
+
+ std::string::size_type last_pos = 0;
+ // Find first separator.
+ std::string::size_type pos = value.find_first_of(separator, last_pos);
+
+ while (pos !=std::string::npos)
+ {
+ // Add to list
+ if (pos == std::string::npos)
+ // Entire string from last_pos
+ ret.push_back(value.substr(last_pos, pos));
+ else
+ // Between pos and last_pos
+ ret.push_back(value.substr(last_pos, pos - last_pos));
+
+ // Find next
+ last_pos = pos + separator.length();
+ pos = value.find_first_of(separator, last_pos);
+ }
+
+ return ret;
+}
+
std::wstring
sbuild::widen_string (std::string const& str,
std::locale locale)
diff --git a/sbuild/sbuild-util.h b/sbuild/sbuild-util.h
index 9597b9e3..0b612ed8 100644
--- a/sbuild/sbuild-util.h
+++ b/sbuild/sbuild-util.h
@@ -115,7 +115,10 @@ namespace sbuild
/**
* Split a string into a string_list. The string is split using
- * separator as a delimiter.
+ * separator as a delimiter. Note that only non-zero-length strings
+ * are preserved, so multiple concatenated delimiters or delimiters
+ * at the beginning and end of the string will not result in empty
+ * strings in the list.
*
* @param value the string to split.
* @param separator the delimiting character or characters.
@@ -155,6 +158,55 @@ namespace sbuild
std::string const& separator);
/**
+ * Split a string into a string_list. The string is split using
+ * separator as a delimiter. All delimiters are used as string
+ * separators, so delimiters at the beginning or end of a string, or
+ * which are concatenated together, will result in empty strings in
+ * the string list.
+ *
+ * @param value the string to split.
+ * @param separator the delimiting character or characters.
+ * @returns a string_list.
+ *
+ * @todo Provide an alternative that splits the string in place
+ * using an iterator interface.
+ */
+ template <typename S>
+ std::vector<S>
+ split_string_strict (S const& value,
+ S const& separator)
+ {
+ std::vector<S> ret;
+
+ // Skip any separators at the start
+ typename S::size_type last_pos = 0;
+ // Find first separator.
+ typename S::size_type pos = value.find_first_of(separator, last_pos);
+
+ while (pos !=S::npos || last_pos != S::npos)
+ {
+ // Add to list
+ if (pos == std::string::npos)
+ // Entire string from last_pos
+ ret.push_back(value.substr(last_pos, pos));
+ else
+ // Between pos and last_pos
+ ret.push_back(value.substr(last_pos, pos - last_pos));
+
+ // Find next
+ last_pos = pos + separator.length();
+ pos = value.find_first_of(separator, last_pos);
+ }
+
+ return ret;
+ }
+
+ // template
+ std::vector<std::string>
+ split_string_strict (std::string const& value,
+ std::string const& separator);
+
+ /**
* Widen a string. The narrow string is converted into a wide
* string. Note that any conversion error will cause the string to
* be clipped at the point of error.