diff options
author | Alexander Smundak <asmundak@google.com> | 2021-07-13 22:11:03 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-07-13 22:11:03 +0000 |
commit | 4b8d81b41c0d9eb324c3f2d704b849abb5d6c2fb (patch) | |
tree | d4b19cd0e731f76716696d8062655e067510f6d2 | |
parent | 10935cf31508bc502a56a672ba421ca95e3166e5 (diff) | |
parent | 3b25eb1c9808f076e28c15524eb818819263cc4d (diff) | |
download | build-4b8d81b41c0d9eb324c3f2d704b849abb5d6c2fb.tar.gz |
Merge "Add mkpatsubst"
-rw-r--r-- | core/product_config.rbc | 33 | ||||
-rw-r--r-- | tests/run.rbc | 10 |
2 files changed, 43 insertions, 0 deletions
diff --git a/core/product_config.rbc b/core/product_config.rbc index 355d41d47d..8e85c4b406 100644 --- a/core/product_config.rbc +++ b/core/product_config.rbc @@ -433,6 +433,38 @@ def _mkinfo(file, message = ""): """Prints info.""" print(message) + +def __mkpatsubst_word(parsed_pattern,parsed_subst, word): + (before, after) = parsed_pattern + if not word.startswith(before): + return word + if not word.endswith(after): + return word + if len(parsed_subst) < 2: + return parsed_subst[0] + return parsed_subst[0] + word[len(before):len(word) - len(after)] + parsed_subst[1] + + +def _mkpatsubst(pattern, replacement, s): + """Emulates Make's patsubst. + + Tokenizes `s` (unless it is already a list), and then performs a simple + wildcard substitution (in other words, `foo%bar` pattern is equivalent to + the regular expression `^foo(.*)bar$, and the first `%` in replacement is + $1 in regex terms). Escaping % is not supported + """ + if pattern.find("\\") >= 0: + fail("'\\' in pattern is not allowed") + parsed_pattern = pattern.split("%", 1) + words = s if type(s) == "list" else _mkstrip(s).split(" ") + if len(parsed_pattern) == 1: + out_words = [ replacement if x == pattern else x for x in words] + else: + parsed_replacement = replacement.split("%", 1) + out_words = [__mkpatsubst_word(parsed_pattern, parsed_replacement, x) for x in words] + return out_words if type(s) == "list" else " ".join(out_words) + + def _mkstrip(s): """Emulates Make's strip. @@ -507,6 +539,7 @@ rblf = struct( indirect = _indirect, mkinfo = _mkinfo, mkerror = _mkerror, + mkpatsubst = _mkpatsubst, mkwarning = _mkwarning, mkstrip = _mkstrip, mksubst = _mksubst, diff --git a/tests/run.rbc b/tests/run.rbc index c7eaf3b56f..4cda1807bb 100644 --- a/tests/run.rbc +++ b/tests/run.rbc @@ -34,6 +34,16 @@ assert_eq("a b c", rblf.mkstrip(" a b \n c \t")) assert_eq("b1 b2", rblf.mksubst("a", "b", "a1 a2")) assert_eq(["b1", "x2"], rblf.mksubst("a", "b", ["a1", "x2"])) +assert_eq("ABcdYZ", rblf.mkpatsubst("ab%yz", "AB%YZ", "abcdyz")) +assert_eq("bcz", rblf.mkpatsubst("a%z", "A%Z", "bcz")) +assert_eq(["Ay", "Az"], rblf.mkpatsubst("a%", "A%", ["ay", "az"])) +assert_eq("AcZ bcz", rblf.mkpatsubst("a%z", "A%Z", "acz bcz")) +assert_eq("Abcd", rblf.mkpatsubst("a%", "A%", "abcd")) +assert_eq("abcZ", rblf.mkpatsubst("%z", "%Z", "abcz")) +assert_eq("azx b", rblf.mkpatsubst("az", "AZ", "azx b")) +assert_eq(["azx", "b"], rblf.mkpatsubst("az", "AZ", ["azx", "b"])) +assert_eq("ABC", rblf.mkpatsubst("abc", "ABC", "abc")) + globals, config = rblf.product_configuration("test/device", init) assert_eq( { |