bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gawk] docu-bug: translate.awk not correct


From: Steffen Schuler
Subject: [gawk] docu-bug: translate.awk not correct
Date: Tue, 3 Feb 2009 22:04:03 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

Hi Arnold,

here some explanations and a patch for gawk-stable/doc/gawk.texi
to this document bug:

Location of the bug: Chapter 13 of gawk.texi

Before the correction, the following error occurs:

$ echo ab | gawk -f old/translate.awk ab ba
aa

The problem is that in the user-function stranslate() you replace one 
after another the characters from the FROM argument in target by the 
characters in the TO argument. Some replaced characters could be replaced 
again in a later iteration step, which is wrong.

One should replace one character of the "target" argument after another 
which occur in the FROM string by the respective character in the TO 
string, to simulate a parallel replacement of all "target" characters.

After this correction one gets:

$ echo ab | gawk -f new/translate.awk ab ba
ba
$

The patch for gawk.texi looks like:

----------------------8<------------------------------------
--- old/gawk.texi       2009-02-03 21:21:38.000000000 +0100
+++ new/gawk.texi       2009-02-03 21:29:15.000000000 +0100
@@ -21696,21 +21696,24 @@
 # to be spelled out. However, if `to' is shorter than `from',
 # the last character in `to' is used for the rest of `from'.
 
-function stranslate(from, to, target,     lf, lt, t_ar, i, c)
+function stranslate(from, to, target,     lf, lt, ltarget, t_ar, i, c,
+                                                               result)
 @{
     lf = length(from)
     lt = length(to)
+    ltarget = length(target)
     for (i = 1; i <= lt; i++)
         t_ar[substr(from, i, 1)] = substr(to, i, 1)
     if (lt < lf)
         for (; i <= lf; i++)
             t_ar[substr(from, i, 1)] = substr(to, lt, 1)
-    for (i = 1; i <= lf; i++) @{
-        c = substr(from, i, 1)
-        if (index(target, c) > 0)
-            gsub(c, t_ar[c], target)
+    for (i = 1; i <= ltarget; i++) @{
+        c = substr(target, i, 1)
+        if (c in t_ar)
+           c = t_ar[c]
+        result = result c
     @}
-    return target
+    return result
 @}
 
 function translate(from, to)
----------------------8<------------------------------------

I hope I could help you!

Regards,

Steffen

-- 
Steffen Schuler (goedel) <address@hidden>
Key ID: 0x42C5D853 / Key-server: pgp.mit.edu

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]