library(charsio): add to_upper and to_lower
This commit is contained in:
@@ -104,7 +104,32 @@ extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :-
|
|||||||
% - `symbolic_control`
|
% - `symbolic_control`
|
||||||
% - `symbolic_hexadecimal`
|
% - `symbolic_hexadecimal`
|
||||||
% - `upper`
|
% - `upper`
|
||||||
|
% - `to_lower(Lower)`
|
||||||
|
% - `to_upper(Upper)`
|
||||||
% - `whitespace`
|
% - `whitespace`
|
||||||
|
%
|
||||||
|
% An example:
|
||||||
|
%
|
||||||
|
% ```
|
||||||
|
% ?- char_type(a, Type).
|
||||||
|
% Type = alnum
|
||||||
|
% ; Type = alpha
|
||||||
|
% ; Type = alphabetic
|
||||||
|
% ; Type = alphanumeric
|
||||||
|
% ; Type = ascii
|
||||||
|
% ; Type = ascii_graphic
|
||||||
|
% ; Type = hexadecimal_digit
|
||||||
|
% ; Type = lower
|
||||||
|
% ; Type = octet
|
||||||
|
% ; Type = prolog
|
||||||
|
% ; Type = symbolic_control
|
||||||
|
% ; Type = to_lower("a")
|
||||||
|
% ; Type = to_upper("A")
|
||||||
|
% ; false.
|
||||||
|
% ```
|
||||||
|
%
|
||||||
|
% Note that uppercase and lowercase transformations use a string. This is because
|
||||||
|
% some characters do not map 1:1 between lowercase and uppercase.
|
||||||
char_type(Char, Type) :-
|
char_type(Char, Type) :-
|
||||||
must_be(character, Char),
|
must_be(character, Char),
|
||||||
( ground(Type) ->
|
( ground(Type) ->
|
||||||
@@ -142,6 +167,8 @@ ctype(sign).
|
|||||||
ctype(solo).
|
ctype(solo).
|
||||||
ctype(symbolic_control).
|
ctype(symbolic_control).
|
||||||
ctype(symbolic_hexadecimal).
|
ctype(symbolic_hexadecimal).
|
||||||
|
ctype(to_lower(_)).
|
||||||
|
ctype(to_upper(_)).
|
||||||
ctype(upper).
|
ctype(upper).
|
||||||
ctype(whitespace).
|
ctype(whitespace).
|
||||||
|
|
||||||
|
|||||||
@@ -2724,9 +2724,10 @@ impl Machine {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
let chars = cell_as_atom!(a2);
|
|
||||||
self.machine_st.fail = true; // This predicate fails by default.
|
self.machine_st.fail = true; // This predicate fails by default.
|
||||||
|
|
||||||
|
read_heap_cell!(a2,
|
||||||
|
(HeapCellValueTag::Atom, (chars, _arity)) => {
|
||||||
macro_rules! macro_check {
|
macro_rules! macro_check {
|
||||||
($id:ident, $name:expr) => {
|
($id:ident, $name:expr) => {
|
||||||
if $id!(c) && chars == $name {
|
if $id!(c) && chars == $name {
|
||||||
@@ -2786,6 +2787,36 @@ impl Machine {
|
|||||||
method_check!(is_uppercase, atom!("upper"));
|
method_check!(is_uppercase, atom!("upper"));
|
||||||
// macro_check!(variable_indicator_char, atom!("variable_indicator"));
|
// macro_check!(variable_indicator_char, atom!("variable_indicator"));
|
||||||
method_check!(is_whitespace, atom!("whitespace"));
|
method_check!(is_whitespace, atom!("whitespace"));
|
||||||
|
}
|
||||||
|
(HeapCellValueTag::Str, s) => {
|
||||||
|
let (name, arity) = cell_as_atom_cell!(self.machine_st.heap[s])
|
||||||
|
.get_name_and_arity();
|
||||||
|
|
||||||
|
match (name, arity) {
|
||||||
|
(atom!("to_upper"), 1) => {
|
||||||
|
let reg = self.machine_st.heap[s+1];
|
||||||
|
let upper_str = self.machine_st.atom_tbl.build_with(&c.to_uppercase().to_string());
|
||||||
|
self.machine_st.unify_complete_string(upper_str, reg);
|
||||||
|
self.machine_st.fail = false;
|
||||||
|
}
|
||||||
|
(atom!("to_lower"), 1) => {
|
||||||
|
let reg = self.machine_st.heap[s+1];
|
||||||
|
let lower_str = self.machine_st.atom_tbl.build_with(&c.to_lowercase().to_string());
|
||||||
|
self.machine_st.unify_complete_string(lower_str, reg);
|
||||||
|
self.machine_st.fail = false;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
|||||||
Reference in New Issue
Block a user