From 7c0f0662d00ba21743fdb2bc538e89065c0765ad Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 19:11:04 +0000 Subject: [PATCH 1/1] stdlib: Implement subst function. --- docs/Goalfile.pod | 13 +++++++++++++ stdlib/prelude.gl | 7 +++++++ tests/10-function-subst.gl | 23 +++++++++++++++++++++++ tests/10-function-subst.sh | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 tests/10-function-subst.gl create mode 100755 tests/10-function-subst.sh diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index da4b142..d58993e 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -12,6 +12,19 @@ Goalfile - introduction, tutorial, and reference for writing goal files =head1 REFERENCE +=head2 Standard Functions + +=head3 subst (from, to, text) + +For example: + + subst ("aa", "AA", "aabbccaa") ⇒ "AAbbccAA" + subst ("a.*c", "b", "aaacac") ⇒ "bb" + +This function works something like make’s C function, except +that C is a regular expression, specifically a L +extended regular expression. + =head1 SEE ALSO L. diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index a73ef1a..8315b6f 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -59,6 +59,13 @@ pure function sort (xs) returning strings = @{ for f in %xs; do echo "$f"; done | sort -u } +# Substitute. +pure function subst (from, to, text) returning string = @{ + # We need to replace any / characters in ‘to’ with escaped ones. + to="$( echo -n %to | sed 's,/,\\/,g' )" + echo %text | sed -E s/%from/$to/g +} + #---------------------------------------------------------------------- # File functions. diff --git a/tests/10-function-subst.gl b/tests/10-function-subst.gl new file mode 100644 index 0000000..4124637 --- /dev/null +++ b/tests/10-function-subst.gl @@ -0,0 +1,23 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Test the wildcard function. + +let s = subst ("a.c", "A/C", "abcdefaacdef") + +goal all = { echo %s } diff --git a/tests/10-function-subst.sh b/tests/10-function-subst.sh new file mode 100755 index 0000000..cbc8907 --- /dev/null +++ b/tests/10-function-subst.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-subst.gl > 10-function-subst.out +test "$(cat 10-function-subst.out)" = "A/CdefA/Cdef" +rm 10-function-subst.out -- 1.8.3.1