[OpenBIOS] [PATCH 2/7] string.fs: add functions to support ROT13 string encoding/decoding

Segher Boessenkool segher at kernel.crashing.org
Sat Feb 13 02:32:38 CET 2016


Let's factor this a bit better...

: rot13 ( c - c )
  dup upc [char] A [char] M between IF d# 13 + EXIT THEN
  dup upc [char] N [char] Z between IF d# 13 - THEN ;

: $rot13 ( str len -- )  bounds ?DO i c@ rot13 i c! LOOP ;

: rot13-str ( str len -- newstr len )  strdup 2dup $rot13 ;


Hope I didn't mess this up ;-)


Segher


On Fri, Feb 05, 2016 at 06:09:01PM +0000, Mark Cave-Ayland wrote:
> diff --git a/openbios-devel/forth/lib/string.fs b/openbios-devel/forth/lib/string.fs
> index eb64749..97b109c 100644
> --- a/openbios-devel/forth/lib/string.fs
> +++ b/openbios-devel/forth/lib/string.fs
> @@ -125,3 +125,31 @@
>  : parse-hex ( str len -- value )
>    base @ hex -rot $number if 0 then swap base !
>  ;
> +
> +
> +\ -----------------------------------------------------
> +\ miscellaneous functions
> +\ -----------------------------------------------------
> +
> +: rot13-c! ( c-addr -- )
> +  \ ROT13 the char at the given address
> +  dup c@    \ ( c-addr char )
> +  dup h# 41 >= over h# 5a <= and if    \ A-Z
> +    h# 41 - h# 0d + h# 1a mod h# 41 + swap c!
> +  else
> +    dup h# 61 >= over h# 7a <= and if    \ a-z
> +      h# 61 - h# 0d + h# 1a mod h# 61 + swap c!
> +    else
> +      2drop
> +    then
> +  then
> +;
> +
> +: rot13-str ( str len -- newstr len )
> +  \ Return a copy of the string encoded ROT13
> +  strdup 0 begin
> +    2 pick over chars + rot13-c!
> +    1+ 2dup = 
> +  until
> +  drop
> +;



More information about the OpenBIOS mailing list