move error handling to Prolog by using the new '$first_non_octet'/2

This commit is contained in:
Markus Triska
2021-11-04 18:30:39 +01:00
parent 8c8c21c63b
commit f668640e3d
3 changed files with 11 additions and 25 deletions

View File

@@ -239,7 +239,10 @@ chars_base64(Cs, Bs, Options) :-
must_be_characters(Bs),
'$chars_base64'(Cs, Bs, Padding, Charset)
; must_be_characters(Cs),
'$chars_base64'(Cs, Bs, Padding, Charset)
( '$first_non_octet'(Cs, N) ->
domain_error(byte_char, N, chars_base64/3)
; '$chars_base64'(Cs, Bs, Padding, Charset)
)
).
must_be_characters(Cs) :-

View File

@@ -390,6 +390,13 @@ format(Fs, Args) :-
format(Stream, Fs, Args) :-
phrase(format_(Fs, Args), Cs),
( stream_property(Stream, type(binary)) ->
( '$first_non_octet'(Cs, N) ->
domain_error(byte_char, N, format/3)
; true
)
; true
),
% we use a specialised internal predicate that uses only a
% single "write" operation for efficiency. It is equivalent to
% maplist(put_char(Stream), Cs). It also works for binary streams.