Browse Source

Implemented lambda based string splitting.

David Piuva 5 years ago
parent
commit
8206e897f3
4 changed files with 34 additions and 8 deletions
  1. 12 6
      Source/DFPSR/base/text.cpp
  2. 7 0
      Source/DFPSR/base/text.h
  3. 2 2
      Source/test.sh
  4. 13 0
      Source/test/tests/StringTest.cpp

+ 12 - 6
Source/DFPSR/base/text.cpp

@@ -902,21 +902,27 @@ void dsr::throwErrorMessage(const String& message) {
 	throw std::runtime_error(message.toStdString());
 }
 
-void dsr::string_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult) {
-	if (!appendResult) {
-		target.clear();
-	}
+void dsr::string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator) {
 	int sectionStart = 0;
 	for (int i = 0; i < source.length(); i++) {
 		DsrChar c = source[i];
 		if (c == separator) {
-			target.push(string_exclusiveRange(source, sectionStart, i));
+			action(string_exclusiveRange(source, sectionStart, i));
 			sectionStart = i + 1;
 		}
 	}
 	if (source.length() > sectionStart) {
-		target.push(string_exclusiveRange(source, sectionStart, source.length()));;
+		action(string_exclusiveRange(source, sectionStart, source.length()));;
+	}
+}
+
+void dsr::string_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult) {
+	if (!appendResult) {
+		target.clear();
 	}
+	string_split_callback([&target](ReadableString section){
+		target.push(section);
+	}, source, separator);
 }
 
 List<ReadableString> dsr::string_split(const ReadableString& source, DsrChar separator) {

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

@@ -30,6 +30,7 @@
 // TODO: Try to hide in the implementation
 #include <iostream>
 #include <sstream>
+#include <functional>
 
 #include "../api/bufferAPI.h"
 #include "../collection/List.h"
@@ -253,6 +254,12 @@ List<ReadableString> string_split(const ReadableString& source, DsrChar separato
 //   If appendResult is false (default), any pre-existing elements in the target list will be cleared before writing the result.
 //   If appendResult is true, the result is appended to the existing target list.
 void string_split_inPlace(List<ReadableString> &target, const ReadableString& source, DsrChar separator, bool appendResult = false);
+// Split a string without needing a list to store the result.
+//   This allow filtering and processing before saving the results,
+//   but makes debugging more difficult and prevents pattern detection.
+// Side-effects:
+//   Calls action for each sub-string divided by separator in source.
+void string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator);
 
 // Post-condition: Returns true iff c is a digit.
 //   Digit <- '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

+ 2 - 2
Source/test.sh

@@ -6,8 +6,8 @@ CPP_VERSION=-std=c++14
 MODE=-DDEBUG
 O_LEVEL=-O2
 
-chmod +x ${ROOT_PATH}/tools/buildAndRun.sh;
-${ROOT_PATH}/tools/buildAndRun.sh "NONE" "NONE" "${ROOT_PATH}" "${TEMP_ROOT}" "NONE" "${MODE}" "${CPP_VERSION}" "${O_LEVEL}";
+chmod +x ${ROOT_PATH}/tools/build.sh;
+${ROOT_PATH}/tools/build.sh "NONE" "NONE" "${ROOT_PATH}" "${TEMP_ROOT}" "NONE" "${MODE}" "${CPP_VERSION}" "${O_LEVEL}";
 if [ $? -ne 0 ]
 then
 	exit 1

+ 13 - 0
Source/test/tests/StringTest.cpp

@@ -272,6 +272,19 @@ START_TEST(String)
 		ASSERT_MATCH(result[0], U" 1 ");
 		ASSERT_MATCH(result[1], U" 2 ");
 	}
+	{ // Callback splitting
+		String numbers = U"1, 3, 5, 7, 9";
+		List<int> result;
+		string_split_callback([&numbers, &result](ReadableString section) {
+			result.push(string_toInteger(section));
+		}, numbers, U',');
+		ASSERT_EQUAL(result.length(), 5);
+		ASSERT_EQUAL(result[0], 1);
+		ASSERT_EQUAL(result[1], 3);
+		ASSERT_EQUAL(result[2], 5);
+		ASSERT_EQUAL(result[3], 7);
+		ASSERT_EQUAL(result[4], 9);
+	}
 	// TODO: Test taking a part of a parent string with a start offset, leaving the parent scope,
 	//       and expanding with append while the buffer isn't shared but has an offset from buffer start.
 	// TODO: Assert that buffers are shared when they should, but prevents side-effects when one is being written to.