sources/mystring.cpp

changeset 196
58d4a48f0904
parent 191
2e6cbacafdc7
child 197
819fdef70d68
--- a/sources/mystring.cpp	Wed Jan 27 19:44:36 2021 +0200
+++ b/sources/mystring.cpp	Wed Jan 27 23:11:41 2021 +0200
@@ -35,7 +35,9 @@
 BEGIN_ZFC_NAMESPACE
 
 /*!
- * \returns a lower-case version of this string.
+ * \brief Converts the given string to lowercase.
+ * \param string string to convert
+ * \return converted string
  */
 std::string to_lowercase(const std::string& string)
 {
@@ -51,9 +53,10 @@
 }
 
 /*!
- * \brief Joins the elements of this string list into one longer string.
- * \param delimeter The delimeter to place between the element strings.
- * \returns the catenated string.
+ * \brief Joins a list of strings to a single string.
+ * \param strings strings to join together
+ * \param delimeter delimeter to join the strings together with
+ * \return joined string
  */
 std::string join_string_list(const std::vector<std::string>& strings, const std::string& delimeter)
 {
@@ -71,38 +74,29 @@
 }
 
 /*!
- * \brief Modifies the given index so that if it is negative, it is translated into a positive index starting from the
- *        end of the string. For example, an index of -1 will be modified to point to the last character in the string,
- *        -2 to the second last, etc.
- * \param index Index to translate.
+ * \brief Extracts a substring from the middle of the given string
+ * \param str string to extract from
+ * \param rangeBegin starting index of the substring
+ * \param rangeEnd end index of the substring
+ * \return substring
  */
-inline void modifyIndex(const std::string& str, int& index)
+std::string mid(const std::string& str, int rangeBegin, int rangeEnd)
 {
-	if (index < 0)
-		index = str.length() - index;
+	rangeBegin = max(rangeBegin, 0);
+	rangeEnd = min(rangeEnd, static_cast<signed>(str.length()));
+	std::string result;
+	if (rangeEnd > rangeBegin)
+	{
+		result = str.substr(rangeBegin, rangeEnd - rangeBegin);
+	}
+	return result;
 }
 
 /*!
- * \param a Starting index of the range.
- * \param b Ending index of the range.
- * \returns a sub-string containing all characters from \c a to \c b, not including the character at \c b.
- */
-std::string mid(const std::string& str, int rangeBegin, int rangeEnd)
-{
-	modifyIndex(str, rangeBegin);
-	modifyIndex(str, rangeEnd);
-	rangeBegin = max(rangeBegin, 0);
-	rangeEnd = min(rangeEnd, static_cast<signed>(str.length()));
-
-	if (rangeEnd <= rangeBegin)
-		return "";
-	else
-		return str.substr(rangeBegin, rangeEnd - rangeBegin);
-}
-
-/*!
- * \param length Amount of characters to return.
- * \returns the \c length right-most characters of the string.
+ * \brief right Extracts a substring from the end of the substring
+ * \param str string to extract from
+ * \param length desired length of the new substring
+ * \return substring
  */
 std::string right(const std::string& str, int length)
 {
@@ -113,22 +107,20 @@
 }
 
 /*!
- * \brief Formats this string using \c vsnprintf, using the provided arguments.
- * \param formatString Template string to use with formatting.
- * \param args Variadic arguments to use with formatting.
+ * \brief Formats a string using \c vsnprintf, using a va_list argument list
+ * \param formatString formatting string
+ * \param args argument list
+ * \return formatted string
  */
 std::string vsprintf(const char* formatString, va_list args)
 {
 	std::string result;
-
 	// Copy the argument list so that we have something to provide to vsnprintf in case we have to call it again.
 	va_list argsCopy;
 	va_copy(argsCopy, args);
-
 	// First, attempt to format using a fixed-size buffer.
 	static char buffer[1024];
-	size_t length = vsnprintf(buffer, sizeof buffer, formatString, args);
-
+	const std::size_t length = std::vsnprintf(buffer, countof(buffer), formatString, args);
 	if (length < sizeof buffer)
 	{
 		// vsnprintf succeeded in fitting the formatted string into the buffer, so we're done.
@@ -137,18 +129,17 @@
 	else
 	{
 		// vsnprintf needs more space, so we have to allocate a new buffer and try again.
-		std::vector<char> newBuffer(length + 1);
-		vsnprintf(newBuffer.data(), length + 1, formatString, argsCopy);
-		result = newBuffer.data();
+		result.resize(length + 1);
+		std::vsnprintf(result.data(), length + 1, formatString, argsCopy);
 	}
-	
 	return result;
 }
 
 /*!
- * \brief Formats this string using \c printf -like syntax.
- * \param formatString Template string to use with formatting.
- * \param ... Variadic arguments to use with formatting.
+ * \brief Formats a string using \c printf -like syntax
+ * \param formatString formatting string
+ * \param ... printf-like arguments
+ * \return formatted string
  */
 std::string __cdecl sprintf(const char* formatString, ...)
 {
@@ -159,6 +150,13 @@
 	return result;
 }
 
+/*!
+ * \brief Removes a substring from the middle of a string
+ * \param string original string
+ * \param start start index to remove
+ * \param end end index to remove
+ * \return modified string
+ */
 std::string remove_range(const std::string &string, int start, int end)
 {
 	std::string result;
@@ -168,21 +166,22 @@
 	return result;
 }
 
-
 /*!
- * \param other Sub-string to find from the beginning of this string.
- * \returns whether or not this string begins with the provided sub-string.
+ * \brief Finds out whether the specified string starts with the specified substring.
+ * \param string string to test
+ * \param substring substring to look for
+ * \return bool
  */
-bool starts_with(const std::string& str, const std::string& other)
+bool starts_with(const std::string& string, const std::string& substring)
 {
-	if (str.length() < other.length())
+	if (string.length() < substring.length())
 		return false;
 	else
-		return std::strncmp(str.data(), other.data(), other.length()) == 0;
+		return std::strncmp(string.data(), substring.data(), substring.length()) == 0;
 }
 
 /*!
- * \brief Replaces all instances of \c text with \c replacement.
+ * \brief Replaces all instances of \c text with \c replacement in place.
  * \param text Text to replace away.
  * \param replacement Text to replace \c text with.
  */
@@ -225,11 +224,10 @@
 }
 
 /*!
- * \brief Converts this string to an integer.
- * \param ok An pointer to a boolean to store whether or not the conversion was successful.
- *           If \c ok is \c NULL, the success state is not stored.
- * \param base The base to interpret this string with.
- * \returns the resulting integer.
+ * \brief Attempts to convert the specified string to an integer.
+ * \param str string to convert
+ * \param base numeric base to convert using.
+ * \return the integer if conversion is successful.
  */
 std::optional<long> to_int(const char* str, int base)
 {
@@ -247,9 +245,9 @@
 }
 
 /*!
- * \brief Removes leading and trailing whitespace from this string. Alternatively a custom filter can be used to strip
- *        something else than whitespace.
- * \param filter The filtering function to use.
+ * \brief Removes unwanted characters from the specified string. Modification is done in place.
+ * \param string String to modify.
+ * \param filter The filtering function to use, defaults to std::isspace in order to remove whitespace.
  */
 void normalize(std::string& string, int (*filter)(int))
 {

mercurial