unescape also Java .properties encoding (\uXXXX): unescape also comments v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 26 Nov 2020 11:41:55 +0100
branchv_0
changeset 18 a8c1381ef103
parent 17 4b1612d20cb2
child 19 967f73af64a4
unescape also Java .properties encoding (\uXXXX): unescape also comments
src/lib/JavaPropertiesUnescapingINIHandler.h
src/lib/UnescapingINIHandler.h
--- a/src/lib/JavaPropertiesUnescapingINIHandler.h	Thu Nov 26 00:38:44 2020 +0100
+++ b/src/lib/JavaPropertiesUnescapingINIHandler.h	Thu Nov 26 11:41:55 2020 +0100
@@ -94,7 +94,7 @@
 
 public:
 
-	JavaPropertiesUnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : UnescapingINIContentHandler(output, lastEscaphingPhase) {
+	JavaPropertiesUnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : UnescapingINIContentHandler(output, lastEscaphingPhase, true) {
 	}
 
 };
--- a/src/lib/UnescapingINIHandler.h	Thu Nov 26 00:38:44 2020 +0100
+++ b/src/lib/UnescapingINIHandler.h	Thu Nov 26 11:41:55 2020 +0100
@@ -31,6 +31,7 @@
 class UnescapingINIContentHandler : public INIContentHandler {
 private:
 	INIContentHandler& output;
+	bool unescapeComments;
 
 protected:
 	const char ESC = '\\';
@@ -53,7 +54,7 @@
 	 * in the last phase, all remaining sequences (including \\) must be recognized and unescaped
 	 * (otherwise the input is considered invalid and an exception is thrown)
 	 */
-	UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase) : output(output), lastEscaphingPhase(lastEscaphingPhase) {
+	UnescapingINIContentHandler(INIContentHandler& output, bool lastEscaphingPhase, bool unescapeComments = false) : output(output), lastEscaphingPhase(lastEscaphingPhase), unescapeComments(unescapeComments) {
 	}
 
 	void startDocument() override {
@@ -67,6 +68,7 @@
 	void startSection(const SectionStartEvent& event) override {
 		SectionStartEvent e = event;
 		e.name = unescape(e.name);
+		if (unescapeComments) e.comment = unescape(e.comment);
 		output.startSection(e);
 	}
 
@@ -80,12 +82,18 @@
 		e.fullKey = unescape(e.fullKey);
 		e.subKey = unescape(e.subKey);
 		e.value = unescape(e.value);
+		if (unescapeComments) e.comment = unescape(e.comment);
 		output.entry(e);
 	}
 
 	void comment(const CommentEvent& event) override {
-		// TODO: optionally unescape also comments (e.g. Java .properties)
-		output.comment(event);
+		if (unescapeComments) {
+			CommentEvent e = event;
+			e.comment = unescape(e.comment);
+			output.comment(e);
+		} else {
+			output.comment(event);
+		}
 	}
 
 	void whitespace(const WhitespaceEvent& event) override {