improved basic escaping v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 13 Dec 2020 16:47:25 +0100
branchv_0
changeset 7 bda9d3c83cb7
parent 6 a823b1aaf1f6
child 8 7c85dc9a310b
improved basic escaping
src/BasicEscapingProcessor.h
src/INIDispatchHandler.h
--- a/src/BasicEscapingProcessor.h	Sat Dec 12 23:09:50 2020 +0100
+++ b/src/BasicEscapingProcessor.h	Sun Dec 13 16:47:25 2020 +0100
@@ -34,14 +34,20 @@
 	relpipe::common::type::StringX escape(const relpipe::common::type::StringX& s, const TextType textType, const QuotingType quotingType) override {
 		std::wstringstream result;
 
+		bool isKey = textType == TextType::EntryKey || textType == TextType::EntrySubKey;
+		bool isSection = textType == TextType::SectionName || textType == TextType::SectionTag;
+		bool isComment = textType == TextType::EntryComment || textType == TextType::SectionComment || textType == TextType::StandaloneComment;
+
 		for (auto ch : s) {
 			if (ch == L'\\') result.put(ESC).put(ESC);
 			else if (ch == L'\n') result.put(ESC).put(L'n');
 			else if (ch == L'\r');
-			else if (ch == L'\t') result.put(ESC).put(L't');
+			else if (isComment) result.put(ch);
 			else if (ch == L'"' && quotingType != QuotingType::Apostrophes) result.put(ESC).put(ch);
 			else if (ch == L'\'' && quotingType != QuotingType::Quotes) result.put(ESC).put(ch);
-			else if (ch == L'=' && quotingType == QuotingType::None) result.put(ESC).put(ch);
+			else if (ch == L'=' && quotingType == QuotingType::None && isKey) result.put(ESC).put(ch);
+			else if (ch == L'[' && quotingType == QuotingType::None && (isKey || isSection)) result.put(ESC).put(ch);
+			else if (ch == L']' && quotingType == QuotingType::None && (isKey || isSection)) result.put(ESC).put(ch);
 			else result.put(ch);
 		}
 
--- a/src/INIDispatchHandler.h	Sat Dec 12 23:09:50 2020 +0100
+++ b/src/INIDispatchHandler.h	Sun Dec 13 16:47:25 2020 +0100
@@ -85,6 +85,7 @@
 public:
 
 	INIDispatchHandler(std::ostream& output, Configuration& configuration) : output(output), writer(output), configuration(configuration) {
+		// TODO: dialects might be moved to configuration (after switching to alt2xml writers)
 		writer.addDialect(std::make_shared<JavaPropertiesDialect>(), dialect::JavaProperties, false);
 		writer.addDialect(std::make_shared<JavaManifestMFDialect>(), dialect::JavaManifestMF, false);
 		writer.addEscapingProcessor(std::make_shared<BasicEscapingProcessor>(), escaping::Basic, true);