From 9a9f8317822645f0f5f760d5879a38326849a5cf Mon Sep 17 00:00:00 2001 From: munawiki Date: Thu, 13 Aug 2026 01:25:47 +0900 Subject: [PATCH] feat: implement split and join expressions (#4463) Co-authored-by: Bart Louwers --- include/mbgl/style/expression/expression.hpp | 3 ++ .../join/empty-array/test.json | 14 +++++++ .../join/empty-item/test.json | 14 +++++++ .../join/empty-separator/test.json | 14 +++++++ .../expression-tests/join/non-array/test.json | 15 +++++++ .../expression-tests/join/numbers/test.json | 15 +++++++ .../expression-tests/join/property/test.json | 23 ++++++++++ .../split/empty-separator/test.json | 14 +++++++ .../split/number-like/test.json | 14 +++++++ .../split/separator-multiple/test.json | 14 +++++++ .../split/separator-not-found/test.json | 14 +++++++ .../style/expression/compound_expression.cpp | 42 +++++++++++++++++++ 12 files changed, 196 insertions(+) create mode 100644 metrics/integration/expression-tests/join/empty-array/test.json create mode 100644 metrics/integration/expression-tests/join/empty-item/test.json create mode 100644 metrics/integration/expression-tests/join/empty-separator/test.json create mode 100644 metrics/integration/expression-tests/join/non-array/test.json create mode 100644 metrics/integration/expression-tests/join/numbers/test.json create mode 100644 metrics/integration/expression-tests/join/property/test.json create mode 100644 metrics/integration/expression-tests/split/empty-separator/test.json create mode 100644 metrics/integration/expression-tests/split/number-like/test.json create mode 100644 metrics/integration/expression-tests/split/separator-multiple/test.json create mode 100644 metrics/integration/expression-tests/split/separator-not-found/test.json diff --git a/include/mbgl/style/expression/expression.hpp b/include/mbgl/style/expression/expression.hpp index b9d664243..468e7e685 100644 --- a/include/mbgl/style/expression/expression.hpp +++ b/include/mbgl/style/expression/expression.hpp @@ -155,6 +155,9 @@ public: EvaluationResult(const std::array& arr) : Result(toExpressionValue(arr)) {} + EvaluationResult(const std::vector& values) + : Result(toExpressionValue(values)) {} + // used only for the special (private) "error" expression EvaluationResult(const type::ErrorType&) noexcept { assert(false); } }; diff --git a/metrics/integration/expression-tests/join/empty-array/test.json b/metrics/integration/expression-tests/join/empty-array/test.json new file mode 100644 index 000000000..b6f1949f9 --- /dev/null +++ b/metrics/integration/expression-tests/join/empty-array/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["join", ["literal", []], ","], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "string" + }, + "outputs": [""], + "serialized": "" + } +} diff --git a/metrics/integration/expression-tests/join/empty-item/test.json b/metrics/integration/expression-tests/join/empty-item/test.json new file mode 100644 index 000000000..d915e3e23 --- /dev/null +++ b/metrics/integration/expression-tests/join/empty-item/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["join", ["literal", ["", "latitude", "", "", "longitude", ""]], ","], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "string" + }, + "outputs": [",latitude,,,longitude,"], + "serialized": ",latitude,,,longitude," + } +} diff --git a/metrics/integration/expression-tests/join/empty-separator/test.json b/metrics/integration/expression-tests/join/empty-separator/test.json new file mode 100644 index 000000000..1aa6a5749 --- /dev/null +++ b/metrics/integration/expression-tests/join/empty-separator/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["join", ["literal", ["latitude", "longitude"]], ""], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "string" + }, + "outputs": ["latitudelongitude"], + "serialized": "latitudelongitude" + } +} diff --git a/metrics/integration/expression-tests/join/non-array/test.json b/metrics/integration/expression-tests/join/non-array/test.json new file mode 100644 index 000000000..0c938bf5f --- /dev/null +++ b/metrics/integration/expression-tests/join/non-array/test.json @@ -0,0 +1,15 @@ +{ + "expression": ["join", "1+2+3", "+"], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "error", + "errors": [ + { + "key": "", + "error": "Expected arguments of type (array, string), but found (string, string) instead." + } + ] + } + } +} diff --git a/metrics/integration/expression-tests/join/numbers/test.json b/metrics/integration/expression-tests/join/numbers/test.json new file mode 100644 index 000000000..d9de8ff0e --- /dev/null +++ b/metrics/integration/expression-tests/join/numbers/test.json @@ -0,0 +1,15 @@ +{ + "expression": ["join", ["literal", [1, 2, 3]], "+"], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "error", + "errors": [ + { + "key": "", + "error": "Expected arguments of type (array, string), but found (array, string) instead." + } + ] + } + } +} diff --git a/metrics/integration/expression-tests/join/property/test.json b/metrics/integration/expression-tests/join/property/test.json new file mode 100644 index 000000000..1b2ecdf3d --- /dev/null +++ b/metrics/integration/expression-tests/join/property/test.json @@ -0,0 +1,23 @@ +{ + "expression": ["join", ["get", "haystack"], "+"], + "inputs": [ + [ + {}, + { + "properties": { + "haystack": ["1", "2", "3"] + } + } + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": false, + "isZoomConstant": true, + "type": "string" + }, + "outputs": ["1+2+3"], + "serialized": ["join", ["array", "string", ["get", "haystack"]], "+"] + } +} diff --git a/metrics/integration/expression-tests/split/empty-separator/test.json b/metrics/integration/expression-tests/split/empty-separator/test.json new file mode 100644 index 000000000..b7bad64e4 --- /dev/null +++ b/metrics/integration/expression-tests/split/empty-separator/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["split", "string", ""], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [["s", "t", "r", "i", "n", "g"]], + "serialized": ["literal", ["s", "t", "r", "i", "n", "g"]] + } +} diff --git a/metrics/integration/expression-tests/split/number-like/test.json b/metrics/integration/expression-tests/split/number-like/test.json new file mode 100644 index 000000000..6d6d704de --- /dev/null +++ b/metrics/integration/expression-tests/split/number-like/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["split", "1+2+3+4", "+"], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [["1", "2", "3", "4"]], + "serialized": ["literal", ["1", "2", "3", "4"]] + } +} diff --git a/metrics/integration/expression-tests/split/separator-multiple/test.json b/metrics/integration/expression-tests/split/separator-multiple/test.json new file mode 100644 index 000000000..977a4b192 --- /dev/null +++ b/metrics/integration/expression-tests/split/separator-multiple/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["split", "needle", "e"], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [["n", "", "dl", ""]], + "serialized": ["literal", ["n", "", "dl", ""]] + } +} diff --git a/metrics/integration/expression-tests/split/separator-not-found/test.json b/metrics/integration/expression-tests/split/separator-not-found/test.json new file mode 100644 index 000000000..b491b60c2 --- /dev/null +++ b/metrics/integration/expression-tests/split/separator-not-found/test.json @@ -0,0 +1,14 @@ +{ + "expression": ["split", "needle", "haystack"], + "inputs": [[{}, {}]], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [["needle"]], + "serialized": ["literal", ["needle"]] + } +} diff --git a/src/mbgl/style/expression/compound_expression.cpp b/src/mbgl/style/expression/compound_expression.cpp index deff59c5f..2a9be693b 100644 --- a/src/mbgl/style/expression/compound_expression.cpp +++ b/src/mbgl/style/expression/compound_expression.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -717,6 +718,45 @@ const auto& concatCompoundExpression() { return signature; } +const auto& splitCompoundExpression() { + static auto signature = detail::makeSignature( + "split", [](const std::string& input, const std::string& delimiter) -> Result> { + std::vector result; + + if (delimiter.empty()) { + std::string_view remaining(input); + result.reserve(unicodeLengthOnValidatedUtf8(remaining)); + while (!remaining.empty()) { + const auto characterLength = getUnicodeCharacterOffsetOnValidatedUtf8(remaining, 1); + result.emplace_back(remaining.substr(0, characterLength)); + remaining.remove_prefix(characterLength); + } + return result; + } + + std::size_t start = 0; + while (true) { + const auto position = input.find(delimiter, start); + if (position == std::string::npos) { + result.emplace_back(input.substr(start)); + break; + } + result.emplace_back(input.substr(start, position - start)); + start = position + delimiter.size(); + } + return result; + }); + return signature; +} + +const auto& joinCompoundExpression() { + static auto signature = detail::makeSignature( + "join", [](const std::vector& input, const std::string& delimiter) -> Result { + return boost::algorithm::join(input, delimiter); + }); + return signature; +} + const auto& resolvedLocaleCompoundExpression() { static auto signature = detail::makeSignature( "resolved-locale", [](const Collator& collator) -> Result { return collator.resolvedLocale(); }); @@ -1065,6 +1105,8 @@ constexpr const auto compoundExpressionRegistry = {"upcase", upcaseCompoundExpression}, {"downcase", downcaseCompoundExpression}, {"concat", concatCompoundExpression}, + {"split", splitCompoundExpression}, + {"join", joinCompoundExpression}, {"resolved-locale", resolvedLocaleCompoundExpression}, {"error", errorCompoundExpression}, {"feature-state", featureStateCompoundExpression},