Merge pull request #1107 from triska/fast_chars_test

New type tests: must_be(chars, ...) and can_be(chars, ...)
This commit is contained in:
Mark Thom
2021-11-25 19:01:03 -05:00
committed by GitHub
3 changed files with 36 additions and 37 deletions

View File

@@ -260,18 +260,11 @@ chars_base64(Cs, Bs, Options) :-
; domain_error(charset, Charset, chars_base64/3)
),
( var(Cs) ->
must_be_characters(Bs),
must_be(chars, Bs),
'$chars_base64'(Cs, Bs, Padding, Charset)
; must_be_characters(Cs),
; must_be(chars, Cs),
( '$first_non_octet'(Cs, N) ->
domain_error(byte_char, N, chars_base64/3)
; '$chars_base64'(Cs, Bs, Padding, Charset)
)
).
must_be_characters(Cs) :-
( partial_string(Cs) ->
true
; must_be(list, Cs),
maplist(must_be(character), Cs)
).

View File

@@ -1,5 +1,5 @@
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written May 2020 by Markus Triska (triska@metalevel.at)
Written 2020, 2021 by Markus Triska (triska@metalevel.at)
Part of Scryer Prolog.
Predicates for cryptographic applications.
@@ -46,7 +46,6 @@
:- use_module(library(format)).
:- use_module(library(charsio)).
:- use_module(library(si)).
:- use_module(library(iso_ext), [partial_string/1]).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
hex_bytes(?Hex, ?Bytes) is det.
@@ -105,21 +104,12 @@ must_be_bytes(Bytes, Context) :-
must_be_byte_chars(Chars, Context) :-
( partial_string(Chars) ->
( '$first_non_octet'(Chars, F) ->
domain_error(byte_char, F, Context)
; true
)
; must_be(list, Chars),
( member(Char, Chars),
char_code(Char, Code),
\+ between(0, 255, Code) ->
domain_error(byte_char, Char, Context)
; true
)
must_be(chars, Chars),
( '$first_non_octet'(Chars, F) ->
domain_error(byte_char, F, Context)
; true
).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cryptographically secure random numbers
=======================================
@@ -620,11 +610,7 @@ encoding_chars(octet, Bs, Cs) :-
),
must_be_byte_chars(Cs, crypto_encoding).
encoding_chars(utf8, Cs, Cs) :-
( partial_string(Cs) ->
true
; must_be(list, Cs),
maplist(must_be(character), Cs)
).
must_be(chars, Cs).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Digital signatures with Ed25519

View File

@@ -1,3 +1,8 @@
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written September 2018 by Markus Triska (triska@metalevel.at)
I place this code in the public domain. Use it in any way you want.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(error, [must_be/2,
can_be/2,
instantiation_error/1,
@@ -5,12 +10,6 @@
type_error/3
]).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written September 2018 by Markus Triska (triska@metalevel.at)
I place this code in the public domain. Use it in any way you want.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
must_be(Type, Term)
@@ -25,10 +24,12 @@
Currently, the following types are supported:
- integer
- atom
- list
- boolean
- character
- chars
- integer
- list
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
must_be(Type, Term) :-
@@ -45,10 +46,27 @@ must_be_(var, Term) :-
must_be_(integer, Term) :- check_(integer, integer, Term).
must_be_(atom, Term) :- check_(atom, atom, Term).
must_be_(character, T) :- check_(error:character, character, T).
must_be_(chars, Ls) :-
( ground(Ls), '$is_partial_string'(Ls) ->
% The expected case (success) uses a very fast test.
% We cannot use partial_string/1 from library(iso_ext),
% because that library itself imports library(error).
true
; must_be(list, Ls),
all_characters(Ls)
).
must_be_(list, Term) :- check_(error:ilist, list, Term).
must_be_(type, Term) :- check_(error:type, type, Term).
must_be_(boolean, Term) :- check_(error:boolean, boolean, Term).
% We cannot use maplist(must_be(character), Cs), because library(lists)
% uses library(error), so importing it would create a cyclic dependency.
all_characters([]).
all_characters([C|Cs]) :-
must_be(character, C),
all_characters(Cs).
check_(Pred, Type, Term) :-
( var(Term) -> instantiation_error(must_be/2)
; call(Pred, Term) -> true
@@ -69,6 +87,7 @@ type(type).
type(integer).
type(atom).
type(character).
type(chars).
type(list).
type(var).
type(boolean).
@@ -97,6 +116,7 @@ can_be(Type, Term) :-
can_(integer, Term) :- integer(Term).
can_(atom, Term) :- atom(Term).
can_(character, T) :- character(T).
can_(chars, Ls) :- '$is_partial_string'(Ls).
can_(list, Term) :- list_or_partial_list(Term).
can_(boolean, Term) :- boolean(Term).