1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 01:51:47 +02:00

Add builtins.string function.

The function 'builtins.split' takes a POSIX extended regular expression
and an arbitrary string. It returns a list of non-matching substring
interleaved by lists of matched groups of the regular expression.

```nix
with builtins;
assert split "(a)b" "abc"      == [ "" [ "a" ] "c" ];
assert split "([ac])" "abc"    == [ "" [ "a" ] "b" [ "c" ] "" ];
assert split "(a)|(c)" "abc"   == [ "" [ "a" null ] "b" [ null "c" ] "" ];
assert split "([[:upper:]]+)" "  FOO   "
                               == [ "  " [ "FOO" ] "   " ];
```
This commit is contained in:
Nicolas B. Pierron 2017-08-15 18:08:41 +00:00
parent f76e85d8f5
commit b8867a0239
3 changed files with 153 additions and 0 deletions

View file

@ -873,6 +873,43 @@ builtins.sort builtins.lessThan [ 483 249 526 147 42 77 ]
</varlistentry>
<varlistentry><term><function>builtins.split</function>
<replaceable>regex</replaceable> <replaceable>str</replaceable></term>
<listitem><para>Returns a list composed of non matched strings interleaved
with the lists of the <link
xlink:href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04">extended
POSIX regular expression</link> <replaceable>regex</replaceable> matches
of <replaceable>str</replaceable>. Each item in the lists of matched
sequences is a regex group.
<programlisting>
builtins.split "(a)b" "abc"
</programlisting>
Evaluates to <literal>[ "" [ "a" ] "c" ]</literal>.
<programlisting>
builtins.split "([ac])" "abc"
</programlisting>
Evaluates to <literal>[ "" [ "a" ] "b" [ "c" ] "" ]</literal>.
<programlisting>
builtins.split "(a)|(c)" "abc"
</programlisting>
Evaluates to <literal>[ "" [ "a" null ] "b" [ null "c" ] "" ]</literal>.
<programlisting>
builtins.split "([[:upper:]]+)" " FOO "
</programlisting>
Evaluates to <literal>[ " " [ "FOO" ] " " ]</literal>.
</para></listitem>
</varlistentry>
<varlistentry><term><function>builtins.stringLength</function>
<replaceable>e</replaceable></term>