Gauche > Archives > 2016/05/21

2016/05/21 03:14:36 UTCshiro
#
srfi-128のcomparator-register-default!は設計ミスの気がしてきたな。拡張したcomparatorを返すmake-extended-comparatorみたいなのと、default-comparatorをパラメータ的にすげ替える仕組みでやった方が良かったんじゃなかろうか。
2016/05/21 09:42:57 UTCg000001
#
text.tr のバックスラッシュの挙動で質問なのですが、from/to-list中でもバックスラッシュを二重に記述する必要があるのは仕様でしょうか
#
(string-tr "\\\\" "\\" "/*")
(string-tr "\\\\" "\x5c" "/*")
;!> *** ERROR: stray backslash in tr spec "\\"


(string-tr "\\\\" "\\\\" "/*")
(string-tr "\\\\" "\x5c\x5c" "/*")
(string-tr "\\\\" "\x5b-\x5d" "{/}")
;=> "//"

(string-tr "//" "/" "\\*")
;=> "**"
#
GNU trとは挙動が違うなと思って質問してみました。バージョンは0.9.5_pre1です :)
2016/05/21 10:00:26 UTCshiro
#
trでもバックスラッシュってエスケープ必要じゃないですか?
#
shiro@scherzo:~/src/Gauche$ tr --version
tr (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.
shiro@scherzo:~/src/Gauche$ echo '/*' | tr '\' '\'
tr: warning: an unescaped backslash at end of string is not portable
tr: warning: an unescaped backslash at end of string is not portable
/*
#
GNU trだとwarningだけで実行はしてくれるみたいですが
2016/05/21 11:01:28 UTCg000001
#
(string-tr "\\" "\\" "/*")
(string-tr "\\" "\x5c" "/*")
;=> "/"
#
を期待しているのですが、実際には
#
(string-tr "\\" "\\\\" "/*")
(string-tr "\\" "\x5c\5c" "/*")
;=> "/"
#
と書かないといけないなという所で、
#
trだと
#
echo '\'|tr '\\' '/*'
echo '\'|tr '\5c' '/*'
2016/05/21 11:05:07 UTCshiro
#
文字列リテラルがバックスラッシュエスケープを解釈して、その結果をtrがまた解釈します。
2016/05/21 11:06:08 UTCg000001
#
あ、後半は、echo '\'|tr '\\x5c' '/*'でした
2016/05/21 11:06:33 UTCshiro
#
"\x5c" も文字列リテラルリーダの時点でバックスラッシュひとつの文字列になるので、"\\" と同じく、trが見るのはバックスラッシュひとつだけになります。
2016/05/21 11:07:41 UTCg000001
#
なるほど、なるほど
2016/05/21 11:08:01 UTCshiro
#
"\\5c" とすればtrには \x5c が渡ります
#
gosh> (string-tr "\\" "\\5c" "/*")
"\\"
#
"\\\\" としても trに \\ が渡って(エスケープされた)バックスラッシュになります。
2016/05/21 11:12:44 UTCg000001
#
なるほど、ありがとうございます m(__)m