library(charsio): add to_upper and to_lower

This commit is contained in:
Adrián Arroyo Calle
2023-04-18 18:48:59 +02:00
parent 8937cac47d
commit 4b882c465c
2 changed files with 117 additions and 59 deletions

View File

@@ -104,7 +104,32 @@ extend_var_list_([V|Vs], N, VarList, NewVarList, VarType) :-
% - `symbolic_control`
% - `symbolic_hexadecimal`
% - `upper`
% - `to_lower(Lower)`
% - `to_upper(Upper)`
% - `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) :-
must_be(character, Char),
( ground(Type) ->
@@ -142,6 +167,8 @@ ctype(sign).
ctype(solo).
ctype(symbolic_control).
ctype(symbolic_hexadecimal).
ctype(to_lower(_)).
ctype(to_upper(_)).
ctype(upper).
ctype(whitespace).

View File

@@ -2724,9 +2724,10 @@ impl Machine {
}
);
let chars = cell_as_atom!(a2);
self.machine_st.fail = true; // This predicate fails by default.
read_heap_cell!(a2,
(HeapCellValueTag::Atom, (chars, _arity)) => {
macro_rules! macro_check {
($id:ident, $name:expr) => {
if $id!(c) && chars == $name {
@@ -2786,6 +2787,36 @@ impl Machine {
method_check!(is_uppercase, atom!("upper"));
// macro_check!(variable_indicator_char, atom!("variable_indicator"));
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)]