Browse Source

Made an optional string_length function.

David Piuva 5 years ago
parent
commit
f8fd9f4b7e
3 changed files with 34 additions and 5 deletions
  1. 4 0
      Source/DFPSR/base/text.cpp
  2. 25 0
      Source/DFPSR/base/text.h
  3. 5 5
      Source/test/tests/StringTest.cpp

+ 4 - 0
Source/DFPSR/base/text.cpp

@@ -791,6 +791,10 @@ double dsr::string_toDouble(const ReadableString& source) {
 	}
 }
 
+int dsr::string_length(const ReadableString& source) {
+	return source.length();
+}
+
 int dsr::string_findFirst(const ReadableString& source, DsrChar toFind, int startIndex) {
 	for (int i = startIndex; i < source.length(); i++) {
 		if (source[i] == toFind) {

+ 25 - 0
Source/DFPSR/base/text.h

@@ -198,13 +198,38 @@ std::ostream& string_toStream(std::ostream& target, const T& source) {
 // ---------------- Procedural API ----------------
 
 
+// Post-condition: Returns the length of source.
+//   Example: string_length(U"ABC") == 3
+int string_length(const ReadableString& source);
+// Post-condition: Returns the base-zero index of source's first occurence of toFind, starting from startIndex. Returns -1 if not found.
+//   Example: string_findFirst(U"ABCABCABC", U'A') == 0
+//   Example: string_findFirst(U"ABCABCABC", U'B') == 1
+//   Example: string_findFirst(U"ABCABCABC", U'C') == 2
+//   Example: string_findFirst(U"ABCABCABC", U'D') == -1
 int string_findFirst(const ReadableString& source, DsrChar toFind, int startIndex = 0);
+// Post-condition: Returns the base-zero index of source's last occurence of toFind.  Returns -1 if not found.
+//   Example: string_findLast(U"ABCABCABC", U'A') == 6
+//   Example: string_findLast(U"ABCABCABC", U'B') == 7
+//   Example: string_findLast(U"ABCABCABC", U'C') == 8
+//   Example: string_findLast(U"ABCABCABC", U'D') == -1
 int string_findLast(const ReadableString& source, DsrChar toFind);
+// Post-condition: Returns a sub-string of source from before the character at inclusiveStart to before the character at exclusiveEnd
+//   Example: string_exclusiveRange(U"0123456789", 2, 4) == U"23"
 ReadableString string_exclusiveRange(const ReadableString& source, int inclusiveStart, int exclusiveEnd);
+// Post-condition: Returns a sub-string of source from before the character at inclusiveStart to after the character at inclusiveEnd
+//   Example: string_inclusiveRange(U"0123456789", 2, 4) == U"234"
 ReadableString string_inclusiveRange(const ReadableString& source, int inclusiveStart, int inclusiveEnd);
+// Post-condition: Returns a sub-string of source from the start to before the character at exclusiveEnd
+//   Example: string_before(U"0123456789", 5) == U"01234"
 ReadableString string_before(const ReadableString& source, int exclusiveEnd);
+// Post-condition: Returns a sub-string of source from the start to after the character at inclusiveEnd
+//   Example: string_until(U"0123456789", 5) == U"012345"
 ReadableString string_until(const ReadableString& source, int inclusiveEnd);
+// Post-condition: Returns a sub-string of source from before the character at inclusiveStart to the end
+//   Example: string_from(U"0123456789", 5) == U"56789"
 ReadableString string_from(const ReadableString& source, int inclusiveStart);
+// Post-condition: Returns a sub-string of source from after the character at exclusiveStart to the end
+//   Example: string_after(U"0123456789", 5) == U"6789"
 ReadableString string_after(const ReadableString& source, int exclusiveStart);
 
 // Post-condition:

+ 5 - 5
Source/test/tests/StringTest.cpp

@@ -15,7 +15,7 @@ void fooInPlace(dsr::String& target, const dsr::ReadableString& a, const dsr::Re
 
 dsr::String foo(const dsr::ReadableString& a, const dsr::ReadableString& b) {
 	dsr::String result;
-	result.reserve(a.length() + b.length());
+	result.reserve(string_length(a) + string_length(b));
 	fooInPlace(result, a, b);
 	return result;
 }
@@ -154,26 +154,26 @@ START_TEST(String)
 	{ // Splitting
 		List<ReadableString> result;
 		string_split_inPlace(result, U"a.b.c.d", U'.');
-		ASSERT_EQUAL(result.length(), 4);
+		ASSERT_EQUAL(string_length(result), 4);
 		ASSERT_MATCH(result[0], U"a");
 		ASSERT_MATCH(result[1], U"b");
 		ASSERT_MATCH(result[2], U"c");
 		ASSERT_MATCH(result[3], U"d");
 		String content = U"One Two Three";
 		result = string_split(content, U' ');
-		ASSERT_EQUAL(result.length(), 3);
+		ASSERT_EQUAL(string_length(result), 3);
 		ASSERT_MATCH(result[0], U"One");
 		ASSERT_MATCH(result[1], U"Two");
 		ASSERT_MATCH(result[2], U"Three");
 		string_split_inPlace(result, U"Four.Five", U'.', true);
-		ASSERT_EQUAL(result.length(), 5);
+		ASSERT_EQUAL(string_length(result), 5);
 		ASSERT_MATCH(result[0], U"One");
 		ASSERT_MATCH(result[1], U"Two");
 		ASSERT_MATCH(result[2], U"Three");
 		ASSERT_MATCH(result[3], U"Four");
 		ASSERT_MATCH(result[4], U"Five");
 		string_split_inPlace(result, U" 1 | 2 ", U'|');
-		ASSERT_EQUAL(result.length(), 2);
+		ASSERT_EQUAL(string_length(result), 2);
 		ASSERT_MATCH(result[0], U" 1 ");
 		ASSERT_MATCH(result[1], U" 2 ");
 	}