1:- module(onepointfour_basics_stringy_length, 2 [ 3 stringy_length/2 4 ,stringy_length/3 5 ,stringy_length_type/3 6 ,stringy_length_type/4 7 ]). 8 9:- use_module(library('onepointfour_basics/checks.pl')). 10 11/* MIT License Follows (https://opensource.org/licenses/MIT) 12 13 Copyright 2021 David Tonhofer <ronerycoder@gluino.name> 14 15 Permission is hereby granted, free of charge, to any person obtaining 16 a copy of this software and associated documentation files 17 (the "Software"), to deal in the Software without restriction, 18 including without limitation the rights to use, copy, modify, merge, 19 publish, distribute, sublicense, and/or sell copies of the Software, 20 and to permit persons to whom the Software is furnished to do so, 21 subject to the following conditions: 22 23 The above copyright notice and this permission notice shall be 24 included in all copies or substantial portions of the Software. 25 26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 29 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 30 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 31 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 32 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33*/
This is not really needed a atom_length/2 and string_length/2 work for both strings and atoms, but it removes the specificity of calling atom_length/2 or string_length/2.
Length may be instantiated to a integer. In that case, the predicate verifies the length of Stringy against Length.
This predicates behaves softly, i.e. preferentially fails on bad input.
48stringy_length(Stringy,Length) :-
49 stringy_length_type(Stringy,Length,_,soft).
hard
will make the predicate throw if Length is bound but negative.
56stringy_length(Stringy,Length,Tuned) :-
57 stringy_length_type(Stringy,Length,_,Tuned).
atom
or string
.
64stringy_length_type(Stringy,Length,Type) :-
65 stringy_length_type(Stringy,Length,Type,soft).
hard
will make the predicate throw if Type is bound but not one
of string
, atom
(and also if Length is bound but negative).73stringy_length_type(Stringy,Length,Type,Tuned) :- 74 check_that(Stringy,[hard(nonvar),hard(stringy)]), 75 check_that(Length,[break(var),hard(integer),tuned(pos0int)],Tuned), 76 check_that(Type,[break(var),tuned(stringy_typeid)],Tuned), 77 stringy_length_decide(Stringy,Length,Type). 78 79stringy_length_decide(Stringy,Length,atom) :- atom(Stringy),!,atom_length(Stringy,Length). 80stringy_length_decide(Stringy,Length,string) :- string(Stringy),!,string_length(Stringy,Length)