Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #45615

Reverse a string (with xchars)

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Reverse a string (with xchars)
Date 2016-02-19 09:00 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2016Feb19.100032@mips.complang.tuwien.ac.at> (permalink)

Show all headers | View raw


The Rosettacode task posted recently was actually
<http://www.rosettacode.org/wiki/Reverse_words_in_a_string>, but there
is also <http://www.rosettacode.org/wiki/Reverse_a_string>, which
reverses the string character-by-character.  There are Forth-94
solutions given there, but with 8-bit or 16-bit characters, these
cannot handle Unicode.

The task is interesting because it is one of the few cases where we
actually need to do something special when dealing with UTF-8, i.e.,
where we actually need words from the xchars wordset for dealing with
UTF-8 (in most other cases, the Forth-94 words are sufficient).

Here's my solution:

\ Has an environmental dependency on 1 chars=1.

\ The xchars wordset offers several ways to skin this cat; I use one,
\ but a different one may be better.

: xreverse {: c-addr u -- c-addr2 u :}
    u allocate throw u + c-addr swap over u + >r begin ( from to r:end)
	over r@ u< while
	    over r@ over - x-size dup >r - 2dup r@ cmove
	    swap r> + swap repeat
    r> drop nip u ;

\ Test case
s" ώщыē" xreverse type

The task description says: "For extra credit, preserve Unicode
combining characters."  Combining characters consist of several code
points; e.g., "ü" is a precomposed character and has one code point,
but could also be encoded in a decomposed way as a "u" followed by the
combining diacritic character U+308 "diaresis".  Because the current
version reverses the code points, it does not work correctly for
decomposed characters.

What is missing from xchars for the extra credit is a way to recognize
combining characters.

While I am at it: a word for validating and normalizing UTF-8 input
would also be a good idea (or split these tasks into several words?).

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2015: http://www.rigwit.co.uk/EuroForth2015/

Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Reverse a string (with xchars) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-19 09:00 +0000
  Re: Reverse a string (with xchars) mentificium@gmail.com - 2016-02-20 23:44 -0800
  Re: Reverse a string (with xchars) Chris Curl <ccurl609@gmail.com> - 2016-02-21 10:18 -0800
    Re: Reverse a string (with xchars) Chris Curl <ccurl609@gmail.com> - 2016-02-21 10:24 -0800
      Re: Reverse a string (with xchars) Chris Curl <ccurl609@gmail.com> - 2016-02-21 10:35 -0800
        Re: Reverse a string (with xchars) Paul Rubin <no.email@nospam.invalid> - 2016-02-21 12:07 -0800
          Re: Reverse a string (with xchars) Chris Curl <ccurl609@gmail.com> - 2016-02-21 12:40 -0800
            Re: Reverse a string (with xchars) Paul Rubin <no.email@nospam.invalid> - 2016-02-21 12:50 -0800
            Re: Reverse a string (with xchars) Paul Rubin <no.email@nospam.invalid> - 2016-02-21 12:56 -0800
              Re: Reverse a string (with xchars) Chris Curl <ccurl609@gmail.com> - 2016-02-21 14:43 -0800
                Re: Reverse a string (with xchars) Paul Rubin <no.email@nospam.invalid> - 2016-02-21 14:59 -0800
                Re: Reverse a string (with xchars) Chris Curl <ccurl609@gmail.com> - 2016-02-21 16:55 -0800
              Re: Reverse a string (with xchars) Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-22 09:37 +0000

csiph-web