From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] awk, not utf aware...
Date: Tue, 26 Feb 2008 15:24:00 -0500 [thread overview]
Message-ID: <7f50f59eb69c5e678cbb97e2b978b112@quanstro.net> (raw)
In-Reply-To: <599f06db0802260418m1c2732fdt1487051c59152e27@mail.gmail.com>
> I think this has come up before, but I didn't found reply.
> If I do in awk something like:
>
> split($0, c, "");
>
> c should be an array of Runes internally, UTF externally, but apparently,
> it is not. Is it just broken?, is there a replacement?, is it just the
> builtins or
> is the whole awk broken?.
i think the comments about this problem are missing the point
a bit. utf8 should be transparent to awk unless the situation demands
that awk needs to know the length of a character. it's not necessary
to keep strings as Rune*s internally to work with utf8. splitting on
"" is a special case where awk does need to know the length of
a character. e.g. this script should work fine
; cat /tmp/smile
#!/bin/awk -f
{
n = split($0, c, "☺");
for(i = 1; i <= n; i++)
print c[i]
}
; echo fu☺bar|/tmp/smile
fu
bar
but splitting on "" won't. i attached a patch that fixes this problem
as an illustration. i'm not using utflen because pcc won't see it.
it's an ugly patch.
i don't think i know what a proper fix for awk would be. i wouldn't
think there are many cases like this, but i haven't spent much time
with awk internals.
- erik
------
9diff run.c
/n/sources/plan9//sys/src/cmd/awk/run.c:1191,1196 - run.c:1191,1219
return(False);
}
+ static int
+ utf8len(char *s)
+ {
+ int c, n, i;
+
+ c = *(unsigned char*)s++;
+ if ((c&0xe0) == 0xc0)
+ n = 2;
+ else if ((c&0xf0) == 0xe0)
+ n = 3;
+ else if ((c&0xf8) == 0xf0)
+ n = 4;
+ else
+ return 1; //-1;
+ i = n-1;
+ if(strlen(s) < i)
+ return 1; // -1;
+ for(; i-- && (c = *(unsigned char*)s++);)
+ if(0x80 != (c&0xc0))
+ return 1; //-1;
+ return n;
+ }
+
Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
{
Cell *x = 0, *y, *ap;
/n/sources/plan9//sys/src/cmd/awk/run.c:1279,1290 - run.c:1302,1316
s++;
}
} else if (sep == 0) { /* new: split(s, a, "") => 1 char/elem */
- for (n = 0; *s != 0; s++) {
- char buf[2];
+ int i, len;
+ char buf[5];
+ for (n = 0; *s != 0; s += len) {
n++;
sprintf(num, "%d", n);
- buf[0] = *s;
- buf[1] = 0;
+ len = utf8len(s);
+ for(i = 0; i < len; i++)
+ buf[i] = s[i];
+ buf[len] = 0;
if (isdigit(buf[0]))
setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval);
else
next prev parent reply other threads:[~2008-02-26 20:24 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-02-26 12:18 Gorka Guardiola
2008-02-26 13:16 ` Martin Neubauer
2008-02-26 14:54 ` Gorka Guardiola
2008-02-26 20:24 ` erik quanstrom [this message]
2008-02-26 21:08 ` geoff
2008-02-26 21:21 ` Pietro Gagliardi
2008-02-26 21:24 ` erik quanstrom
2008-02-26 21:32 ` Steven Vormwald
2008-02-26 21:40 ` Pietro Gagliardi
2008-02-26 21:42 ` Pietro Gagliardi
2008-02-26 23:59 ` Steven Vormwald
2008-02-27 2:38 ` Joel C. Salomon
2008-02-29 17:00 ` Douglas A. Gwyn
2008-02-26 21:34 ` erik quanstrom
2008-02-27 7:36 ` Gorka Guardiola
2008-02-27 15:54 ` Sape Mullender
2008-02-27 20:01 ` Uriel
2008-02-28 19:06 ` [9fans] localization, unicode, regexps (was: awk, not utf aware...) Tristan Plumb
2008-02-28 15:10 ` [9fans] awk, not utf aware erik quanstrom
2008-03-03 23:48 ` Jack Johnson
2008-03-04 0:13 ` erik quanstrom
2008-02-27 9:57 erik quanstrom
2008-02-28 18:54 Aharon Robbins
2008-02-28 21:48 ` Uriel
2008-02-28 22:08 ` erik quanstrom
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7f50f59eb69c5e678cbb97e2b978b112@quanstro.net \
--to=quanstro@quanstro.net \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).