| 1 |
* Function GREPL |
| 2 |
* replaces all the items in SET1 with all the items in SET2 |
| 3 |
* SET1/2 must have the form "ITEM ITEM ITEM " -- space at the end |
| 4 |
* [email protected] |
| 5 |
* requires "repl.inc" |
| 6 |
* 31.07.2022 |
| 7 |
################################################################# |
| 8 |
# MIT License - Copyright (c) 2022 Guido Milanese |
| 9 |
# See file LICENSE in this package |
| 10 |
################################################################# |
| 11 |
define("grepl(Pass,Set1,Set2)datum,I,N,T1,T2,A1,A2") |
| 12 |
-include "repl.inc" |
| 13 |
:(grepl_end) |
| 14 |
grepl |
| 15 |
grepl_bg |
| 16 |
T1 = TABLE() |
| 17 |
T2 = TABLE() |
| 18 |
N = 0 |
| 19 |
grepl1 N = N + 1 |
| 20 |
Set1 ? break(' ') . datum len(1) rem . Set1 :f(grepl1_nd) |
| 21 |
T1[N] = datum :(grepl1) |
| 22 |
grepl1_nd |
| 23 |
|
| 24 |
N = 0 |
| 25 |
grepl2 N = N + 1 |
| 26 |
Set2 ? break(' ') . datum len(1) rem . Set2 :f(grepl2_nd) |
| 27 |
T2[N] = datum :(grepl2) |
| 28 |
grepl2_nd |
| 29 |
A1 = convert(T1,"ARRAY") |
| 30 |
A2 = convert(T2,"ARRAY") |
| 31 |
grepl3_bg I = I + 1 |
| 32 |
Pass = repl(Pass,A1[I,2],A2[I,2]) :S(grepl3_bg)F(grepl3_nd) |
| 33 |
grepl3_nd |
| 34 |
grepl_rt grepl = Pass :(return) |
| 35 |
grepl_end |
| 36 |
|
| 37 |
********************************************************************* |
| 38 |
* This function, as Phil Budne's "repl", replaces string/string |
| 39 |
* but, as standard "replace", can replace a set with another set. |
| 40 |
* In this way also Unicode chars can be replaced with no limitation |
| 41 |
* since they are treated as strings. |
| 42 |
* Text = "aiuole" |
| 43 |
* S1 = "a e i o u " |
| 44 |
* S2 = "1 2 3 4 5 " |
| 45 |
* Text2 = grepl(Text,S1,S2) |
| 46 |
* will output: "1354l2" |
| 47 |
* But also: |
| 48 |
* Text = "aiuole" |
| 49 |
* S1 = "a e i o u " |
| 50 |
* S2 = "ā ē ī ō ū " |
| 51 |
* will output: "āīūōlē" |
| 52 |
* the standard REPLACE ("aeiou" / "āēīōū") will fail |
| 53 |
* because Unicode chars (>127 ASCII) do not have the same length of chars <127. |
| 54 |
* Step by step: |
| 55 |
* create two tables for sets: T1 and T2 |
| 56 |
* feed the tables scanning thes two sets (items must be separeted by spaces) |
| 57 |
* convert from tables to arrays |
| 58 |
* use REPL converting string/string in PASS: |
| 59 |
* each item of SET1 is replaced by the corresponding item in S2 |
| 60 |
* if SET2 is shorter the exceeding item in SET1 is left unchanged |
| 61 |
* if SET1 is shorter the exceeding item in SET2 is not used |
| 62 |
* Please notice that separating items by spaces makes it possible to substitute |
| 63 |
* also sets of strings, such as e.g. LaTeX accents. |