diff options
author | Piotr Fusik <piotr@fusion-lang.org> | 2024-02-19 10:58:11 +0100 |
---|---|---|
committer | Piotr Fusik <piotr@fusion-lang.org> | 2024-02-19 10:58:11 +0100 |
commit | 82c353d059397504cb5df13510a05e11add5110a (patch) | |
tree | 0151309a94f67ff45e039449712bde82e7196818 | |
parent | 0c757015a6ae2dfefe2fa21a21d253e2cbc4da4a (diff) |
[string] string.ToLower, string.ToUpper in C++ using ICU.
#143
-rw-r--r-- | GenCpp.fu | 27 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libfut.cpp | 27 | ||||
-rw-r--r-- | libfut.cs | 27 | ||||
-rw-r--r-- | libfut.hpp | 1 | ||||
-rw-r--r-- | libfut.js | 27 | ||||
-rw-r--r-- | test/StringToLower.fu | 12 | ||||
-rw-r--r-- | test/StringToUpper.fu | 12 |
8 files changed, 58 insertions, 77 deletions
@@ -568,6 +568,15 @@ public class GenCpp : GenCCpp WriteArgsInParentheses(method, args); } + void WriteStringToLowerUpper!(FuExpr obj, string name) + { + Include("string"); + Include("unicode/unistr.h"); + Write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to"); + Write(name); + WriteCall("er().toUTF8String(result); }", obj); + } + void WriteAllAnyContains!(string function, FuExpr obj, List<FuExpr#> args) { Include("algorithm"); @@ -809,24 +818,10 @@ public class GenCpp : GenCCpp WriteStringMethod(obj, "substr", method, args); break; case FuId.StringToLower: - Include("algorithm"); - Include("cctype"); - Write("[&] { std::string data = std::string{"); - obj.Accept(this, FuPriority.Argument); - Write("}; "); - Write("std::transform(data.begin(), data.end(), data.begin(), "); - Write("[](unsigned char c) { return std::tolower(c); }); "); - Write("return data; }()"); + WriteStringToLowerUpper(obj, "Low"); break; case FuId.StringToUpper: - Include("algorithm"); - Include("cctype"); - Write("[&] { std::string data = std::string{"); - obj.Accept(this, FuPriority.Argument); - Write("}; "); - Write("std::transform(data.begin(), data.end(), data.begin(), "); - Write("[](unsigned char c) { return std::toupper(c); }); "); - Write("return data; }()"); + WriteStringToLowerUpper(obj, "Upp"); break; case FuId.ArrayBinarySearchAll: case FuId.ArrayBinarySearchPart: @@ -181,7 +181,7 @@ test/bin/%/c.exe: test/bin/%/Test.c $(DO)$(CC) -o $@ $(TEST_CFLAGS) -Wno-unused-function -I $(<D) $^ `pkg-config --cflags --libs glib-2.0` -lm || grep '//FAIL:.*\<c\>' test/$*.fu test/bin/%/cpp.exe: test/bin/%/Test.cpp - $(DO)$(CXX) -o $@ $(TEST_CXXFLAGS) -I $(<D) $^ || grep '//FAIL:.*\<cpp\>' test/$*.fu + $(DO)$(CXX) -o $@ $(TEST_CXXFLAGS) -I $(<D) $^ $(if $(findstring $*, StringToLower StringToUpper), -licuuc) || grep '//FAIL:.*\<cpp\>' test/$*.fu test/bin/%/cs.dll: test/bin/%/Test.cs $(DO)$(CSC) -out:$@ $^ || grep '//FAIL:.*\<cs\>' test/$*.fu @@ -13532,6 +13532,15 @@ void GenCpp::writeStringMethod(const FuExpr * obj, std::string_view name, const writeArgsInParentheses(method, args); } +void GenCpp::writeStringToLowerUpper(const FuExpr * obj, std::string_view name) +{ + include("string"); + include("unicode/unistr.h"); + write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to"); + write(name); + writeCall("er().toUTF8String(result); }", obj); +} + void GenCpp::writeAllAnyContains(std::string_view function, const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args) { include("algorithm"); @@ -13770,24 +13779,10 @@ void GenCpp::writeCallExpr(const FuExpr * obj, const FuMethod * method, const st writeStringMethod(obj, "substr", method, args); break; case FuId::stringToLower: - include("algorithm"); - include("cctype"); - write("[&] { std::string data = std::string{"); - obj->accept(this, FuPriority::argument); - write("}; "); - write("std::transform(data.begin(), data.end(), data.begin(), "); - write("[](unsigned char c) { return std::tolower(c); }); "); - write("return data; }()"); + writeStringToLowerUpper(obj, "Low"); break; case FuId::stringToUpper: - include("algorithm"); - include("cctype"); - write("[&] { std::string data = std::string{"); - obj->accept(this, FuPriority::argument); - write("}; "); - write("std::transform(data.begin(), data.end(), data.begin(), "); - write("[](unsigned char c) { return std::toupper(c); }); "); - write("return data; }()"); + writeStringToLowerUpper(obj, "Upp"); break; case FuId::arrayBinarySearchAll: case FuId::arrayBinarySearchPart: @@ -13882,6 +13882,15 @@ namespace Fusion WriteArgsInParentheses(method, args); } + void WriteStringToLowerUpper(FuExpr obj, string name) + { + Include("string"); + Include("unicode/unistr.h"); + Write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to"); + Write(name); + WriteCall("er().toUTF8String(result); }", obj); + } + void WriteAllAnyContains(string function, FuExpr obj, List<FuExpr> args) { Include("algorithm"); @@ -14119,24 +14128,10 @@ namespace Fusion WriteStringMethod(obj, "substr", method, args); break; case FuId.StringToLower: - Include("algorithm"); - Include("cctype"); - Write("[&] { std::string data = std::string{"); - obj.Accept(this, FuPriority.Argument); - Write("}; "); - Write("std::transform(data.begin(), data.end(), data.begin(), "); - Write("[](unsigned char c) { return std::tolower(c); }); "); - Write("return data; }()"); + WriteStringToLowerUpper(obj, "Low"); break; case FuId.StringToUpper: - Include("algorithm"); - Include("cctype"); - Write("[&] { std::string data = std::string{"); - obj.Accept(this, FuPriority.Argument); - Write("}; "); - Write("std::transform(data.begin(), data.end(), data.begin(), "); - Write("[](unsigned char c) { return std::toupper(c); }); "); - Write("return data; }()"); + WriteStringToLowerUpper(obj, "Upp"); break; case FuId.ArrayBinarySearchAll: case FuId.ArrayBinarySearchPart: @@ -2320,6 +2320,7 @@ private: void writePtrRange(const FuExpr * obj, const FuExpr * index, const FuExpr * count); void writeNotRawStringLiteral(const FuExpr * obj, FuPriority priority); void writeStringMethod(const FuExpr * obj, std::string_view name, const FuMethod * method, const std::vector<std::shared_ptr<FuExpr>> * args); + void writeStringToLowerUpper(const FuExpr * obj, std::string_view name); void writeAllAnyContains(std::string_view function, const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args); void writeCollectionMethod(const FuExpr * obj, std::string_view name, const std::vector<std::shared_ptr<FuExpr>> * args); void writeCString(const FuExpr * expr); @@ -14312,6 +14312,15 @@ export class GenCpp extends GenCCpp this.writeArgsInParentheses(method, args); } + #writeStringToLowerUpper(obj, name) + { + this.include("string"); + this.include("unicode/unistr.h"); + this.write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to"); + this.write(name); + this.writeCall("er().toUTF8String(result); }", obj); + } + #writeAllAnyContains(function_, obj, args) { this.include("algorithm"); @@ -14551,24 +14560,10 @@ export class GenCpp extends GenCCpp this.#writeStringMethod(obj, "substr", method, args); break; case FuId.STRING_TO_LOWER: - this.include("algorithm"); - this.include("cctype"); - this.write("[&] { std::string data = std::string{"); - obj.accept(this, FuPriority.ARGUMENT); - this.write("}; "); - this.write("std::transform(data.begin(), data.end(), data.begin(), "); - this.write("[](unsigned char c) { return std::tolower(c); }); "); - this.write("return data; }()"); + this.#writeStringToLowerUpper(obj, "Low"); break; case FuId.STRING_TO_UPPER: - this.include("algorithm"); - this.include("cctype"); - this.write("[&] { std::string data = std::string{"); - obj.accept(this, FuPriority.ARGUMENT); - this.write("}; "); - this.write("std::transform(data.begin(), data.end(), data.begin(), "); - this.write("[](unsigned char c) { return std::toupper(c); }); "); - this.write("return data; }()"); + this.#writeStringToLowerUpper(obj, "Upp"); break; case FuId.ARRAY_BINARY_SEARCH_ALL: case FuId.ARRAY_BINARY_SEARCH_PART: diff --git a/test/StringToLower.fu b/test/StringToLower.fu index 5051415..c1827cb 100644 --- a/test/StringToLower.fu +++ b/test/StringToLower.fu @@ -1,4 +1,4 @@ -// https://pastebin.com/fuw4Uizk +// https://pastebin.com/fuw4Uizk // ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ ΟΔΥΣΣΕΥΣ // đâăêôơưấắếốớứầằềồờừậặệộợựш ὀδυσσεύς @@ -8,11 +8,11 @@ public class Test { string() orig1 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"; //FAIL: cl string orig2 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"; - string() new1 = orig1.ToLower(); + string() new1 = orig1.ToLower(); //FAIL: cpp requires ICU string() new2 = orig2.ToLower(); - return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" && - orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" && - new1 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш" && - new2 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш"; + return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" + && orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" + && new1 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш" + && new2 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш"; } } diff --git a/test/StringToUpper.fu b/test/StringToUpper.fu index 1260beb..efd63f4 100644 --- a/test/StringToUpper.fu +++ b/test/StringToUpper.fu @@ -1,4 +1,4 @@ -// https://pastebin.com/fuw4Uizk +// https://pastebin.com/fuw4Uizk // ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ // đâăêôơưấắếốớứầằềồờừậặệộợựш @@ -8,11 +8,11 @@ public class Test { string() orig1 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"; //FAIL: cl string orig2 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"; - string() new1 = orig1.ToUpper(); + string() new1 = orig1.ToUpper(); //FAIL: cpp requires ICU string() new2 = orig2.ToUpper(); - return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" && - orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" && - new1 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ" && - new2 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ"; + return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" + && orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" + && new1 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ" + && new2 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ"; } } |