srfi.207 - 文字列表示のバイトベクタ ¶このSRFIはバイトベクタの別形式の外部表現を定義し、 バイトベクタをバイト列(bytestring)として扱える多くの手続きを提供します。
Gaucheはバイトベクタの「文字列」表現を組み込みでサポートしています。
リーダーは、数値形式(例: #u(65 66 67 68))、
文字列形式(例: #u8"ABCD")、のどちらでも読むことができます。
ライターはデフォルトで数値形式を使いますが、
<write-controls>のbytestringスロットを真にすると
文字列形式を使うようになります。
gosh> #u8(65 66 67 68)
#u8(65 66 67 68)
gosh> ,pm bytestring #t
Current print mode:
length : 50 base : 10 exact-decimal : #f
level : 10 radix-prefix : #f array : compact
pretty : #t string-length : 256 complex : rectangular
width : 79 bytestring : #t
gosh> #u8(65 66 67 68)
#u8"ABCD"
[SRFI-207]{srfi.207}
各引数はそれぞれ以下のいずれかでなければなりません。
手続きは与えられた引数を全部つなげた新たなバイトベクタを作って返します。
上記以外のオブジェクトが引数に与えられた場合はエラーが投げられます。
(bytestring "lor" #\r #\x65 #u8(#x6d)) ⇒ #u8(108 111 114 114 101 109) ; #u8"lorrem"
[SRFI-207]{srfi.207}
lisの要素をつなげた内容を持つバイトベクタを作って返します。
lisの各要素は、0以上255以下の正確な整数、ASCII文字、
バイトベクタ、あるいはASCII文字列のいずれかでなければなりません。
実質的には(apply bytestring lis)と同じです。
[SRFI-207]{srfi.207}
バイトベクタbvの、インデックスat以降を、
lisの要素で埋めます。
lisの各要素は、0以上255以下の正確な整数、ASCII文字、
バイトベクタ、あるいはASCII文字列のいずれかでなければなりません。
戻り値は未定義です。
(rlet1 bv (make-u8vector 10)
(make-bytestring! bv 1 '("ab" #\c #u8"de")))
⇒ #u8(0 97 98 99 100 101 0 0 0 0)
[SRFI-207]{srfi.207}
それぞれ、バイトベクタと十六進数文字列とを相互に変換します。
hex-string->bytevectorが、引数中に十六進数と解釈できない
文字に出会った場合は、bytestring-error?を満たすコンディションが投げられます。
(bytevector->hex-string #u8"aloha") ⇒ "616c6f6861" (hex-string->bytevector "616C6F6861") ⇒ #u8(97 108 111 104 97) ; #u8"aloha"
[SRFI-207]{srfi.207}
それぞれ、バイトベクタとBase64文字列とを相互に変換します。
hex-string->bytevectorが、引数がBase64と解釈できない
場合は、bytestring-error?を満たすコンディションが投げられます。
但し、空白文字は許されて、それは単に無視されます。
デフォルトでは標準のBase64マッピングが使われます。
マッピングの62と63に使う代わりの文字を、2文字のASCII文字列列として
digitsに渡すことで、それらの文字のマッピングを変更できます。
いわゆる「URL安全」なBase64バリエーションを使うには、
"-_"をdigitsに渡します。
Gauche特有のBase64ライブラリには他のAPIも提供されています。
rfc.base64 - Base64エンコーディングを参照してください。
[SRFI-207]{srfi.207}
Converts each element of a bytevector bv to
either a character (if the element is an integer between 32 and 127,
inclusive) or an integer, and returns a list of converted elements.
If you pass the resulting list to make-bytestring, you can obtain
the same bytevector as bv.
If you pass the optional start and end indexes, only the specified range of bv is considered.
[SRFI-207]{srfi.207}
Returns a generator that works as the same as
(uvector->generator (bytestring arg …)),
but this can skip creating intermediate bytestrings.
[SRFI-207]{srfi.207}
Returns a newly allocated bytevector of at least len long.
If bv is shorter than len, the rest is padded with
char-or-u8 (which can be either an ASCII character or
an exact integer between 0 and 255). The pad is added to the left
with bytestring-pad, and to the right with bytestring-pad-right.
This is a bytestring version of string-pad and string-pad-right
(see 文字列の選択).
[SRFI-207]{srfi.207}
Remove octets that satisfies pred
at the beginning / at the end / at both ends of a bitvector bv.
The procedure pred receives an exact integer between 0 and 255.
They always return a fresh bytevector, even when no octets are trimmed.
This is a bytestring version of string-trim,
string-trim-right, and string-trim-both
(see 文字列の選択).
[SRFI-207]{srfi.207}
Returns a new bytevector whose content is a copy of a bytevector
bv1, except the part beginning from the index
start1 (inclusive) and ending at the index end1 (exclusive)
is replaced by a byvetvector bv2.
When optional start2 and end2
arguments are given, bv2 is trimmed first accordingly.
The size of the gap, (- end1 start1), doesn’t
need to be the same as the size of the inserted bytevector.
Effectively, this is the same as the following code.
(bytevector-append (subseq bv1 0 start1)
(subseq bv2 start2 end2)
(subseq bv1 end1 (bytevector-length bv1)))
This is a bytestring version of string-replace
(see 他の文字列操作).
[SRFI-207]{srfi.207}
Compare two bytevectors lexicographically. These are bytestring
version of string<? (see 文字列の色々な比較).
There’s no bytestring=?, for you can check equality of bytevectors
by bytevector=? in scheme.bytevector and
gauche.uvector (see バイトベクタ互換性).
Note that bytevector-comparator compares bytevectors
differently. The comparator first compares the lengths, and only
uses elements when both vectors are of the same length
(see 用意されている比較器).
(bytestring<? #u8"abc" #u8"ac") ⇒ #t (<? bytevector-comparator #u8"abc" #u8"ac") ⇒ #f
This contrasts SRFI-207’s view of bytevectors as a string of bytes, with the view of them as an array of bytes.
[SRFI-207]{srfi.207}
Returns the first or last index of the octet that satisfies pred
in a bytevector bv. The optional start and end
arguments limit the range to search.
These are bytestring version of string-index and
string-index-right (see 文字列の探索).
[SRFI-207]{srfi.207}
Returns two bytevectors, the first one is a maximum prefix of bv
whose element does not / does satisfy pred, and the second one
is the rest of bv.
(bytestring-break #u8"abracadabra" (cut = <> 100)) ⇒ #u8(97 98 114 97 99 97) and #u8(100 97 98 114 97) (bytestring-span #u8(1 2 3 4 5 4 3 2 1) (cut < <> 5)) ⇒ #u8(1 2 3 4) and #u8(5 4 3 2 1)
[SRFI-207]{srfi.207}
Given list of bytevectors bv-list, concatenates the bytevectors
with a delimiter delim, which can be an exact integer
between 0 and 255 inclusive, an ASCII character, a bytevector
or a string of ASCII characters.
The grammar argument must be one of the symbols infix,
strict-infix, suffix, and prefix, as in
string-join (see 文字列を扱うその他の手続き). The difference
between infix and strict-infix is that the former
allows to join zero byvectors (returns #u8()), while the
latter raises an error when bv-list is empty. When omitted,
infix is assumed.
(bytestring-join '(#u8"a" #u8"b" #u8"c") 0) ⇒ #u8(97 0 98 0 99) (bytestring-join '(#u8"a" #u8"b" #u8"c") 0 'prefix) ⇒ #u8(0 97 0 98 0 99) (bytestring-join '(#u8"a" #u8"b" #u8"c") 0 'suffix) ⇒ #u8(97 0 98 0 99 0)
[SRFI-207]{srfi.207}
Split a bytevector bv with delim, which must be
either an exact integer between 0 and 255 inclusive or an ASCII character,
and returns a list of bytevectors. Delimiters themselves are not included
in the result.
The grammar argument is, if given, either one of the symbols
infix, strict-infix, prefix, or suffix,
similar to bytestring-join. If it is prefix/suffix,
the preceding/succeeding delim is ignored, if any. There are no
difference between infix and strict-infix. The default
is infix.
(bytestring-split #u8"abracadabra" #\a) ⇒ (#u8() #u8(98 114) #u8(99) #u8(100) #u8(98 114) #u8()) (bytestring-split #u8"abracadabra" #\a 'prefix) ⇒ (#u8(98 114) #u8(99) #u8(100) #u8(98 114) #u8()) (bytestring-split #u8"abracadabra" #\a 'suffix) ⇒ (#u8() #u8(98 114) #u8(99) #u8(100) #u8(98 114))
[SRFI-207]{srfi.207}
This parses a textual representation of a bytestring read from
port (defaults to the current input port) and returns a bytevector.
If prefix is false, it assumes the preceding "#u8" has
already been read. If the input can’t be parsed as a valid
textual representation of a bytestring, an error satisfying
bytestring-error? is raised.
(with-input-from-string "#u8\"abc\\x80;\"" (cut read-textual-bytestring #t)) ⇒ #u8(97 98 99 128)
[SRFI-207]{srfi.207}
Writes a bytevector bv in the textual representation to
port (defaults to the current output port).
(write-textual-bytestring #u8(65 66 128 255 0))
⇒ prints #u8"AB\x80;\xff;\x00;"
[SRFI-207]{srfi.207}
Works like(write-textual-bytestring (bytestring arg ...) port), but without creating the intermediate bytevector. If any arguments are invalid to construct a bytevector, an error satisfyingbytestring-error?signaled.
[SRFI-207]{srfi.207}
Returns #t if obj is a condition thrown from
bytestring operations.