View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2013-2018, University of Amsterdam
    7                              VU University Amsterdam
    8			      CWI, Amsterdam
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(atom,
   38          [ restyle_identifier/3,               % +Style, +In, +Out
   39            identifier_parts/2,                 % +Identifier, -Parts
   40            join_identifier_parts/3             % +Style, +Parts, -Identifier
   41          ]).   42:- autoload(library(apply),[maplist/2,maplist/3]).   43:- autoload(library(ctypes),[is_upper/1]).   44
   45
   46/** <module> Operations on atoms
   47
   48This library provides operations  on  atoms   that  are  not  covered by
   49builtin predicates. The current implementation is   just a start, making
   50code developed in _xpce_ and duplicated in various projects reusable.
   51*/
   52
   53
   54                 /*******************************
   55                 *      RESTYLE IDENTIFIERS     *
   56                 *******************************/
   57
   58%!  restyle_identifier(+Style, +In, -Out) is det.
   59%
   60%   Restyle an identifier by extracting the alnum substrings and
   61%   joining them together according to Style.
   62%
   63%   @arg Style is one of `'OneTwo'`, `oneTwo`, `one_two`, `'One_Two'` or
   64%   a term style(CapitaliseFirst, CapitaliseRest, Separator).
   65
   66%   @see join_identifier_parts/3.
   67
   68restyle_identifier(Style, In, Out) :-
   69    identifier_parts(In, Parts),
   70    join_identifier_parts(Style, Parts, Out).
   71
   72
   73%!  identifier_parts(+Identifier, -Parts) is det.
   74%
   75%   Parts is a list of atoms  that   make  up  Identifier. The parts
   76%   found are turned into lowercase, unless   all its characters are
   77%   uppercase.  E.g.,
   78%
   79%       ?- identifier_parts('sourceCodeURI', X).
   80%       X = [source, code, 'URI'].
   81
   82identifier_parts(';', [';']) :- !.
   83identifier_parts('|', ['|']) :- !.
   84identifier_parts('!', ['!']) :- !.
   85identifier_parts(',', [',']) :- !.
   86identifier_parts(Name, Parts) :-
   87    atom_codes(Name, Codes),
   88    (   phrase(identifier_parts(Parts), Codes)
   89    ->  true
   90    ;   maplist(is_symbol_code, Codes)
   91    ->  Parts = [Name]
   92    ).
   93
   94is_symbol_code(Code) :-
   95    code_type(Code, prolog_symbol).
   96
   97identifier_parts([H|T]) -->
   98    identifier_part(H),
   99    !,
  100    identifier_parts(T).
  101identifier_parts([]) --> [].
  102
  103identifier_part(H) -->
  104    string(Codes, Tail),
  105    sep(Tail),
  106    !,
  107    { Codes = [_|_],
  108      atom_codes(H0, Codes),
  109      (   maplist(is_upper, Codes)
  110      ->  H = H0
  111      ;   downcase_atom(H0, H)
  112      )
  113    }.
  114
  115string(T,T) --> [].
  116string([H|T], L) --> [H], string(T, L).
  117
  118sep([]) --> sep_char, !, sep_chars.
  119sep([T]), [N] -->
  120    [T,N],
  121    { code_type(T, lower),
  122      code_type(N, upper)
  123    }.
  124sep([],[],[]).
  125
  126sep_char -->
  127    [H],
  128    { \+ code_type(H, alnum) }.
  129
  130sep_chars --> sep_char, !, sep_chars.
  131sep_chars --> [].
  132
  133%!  join_identifier_parts(+Style, +Parts, -Identifier)
  134%
  135%   Join parts of an identifier according to Style. Style is one of:
  136%
  137%     - 'OneTwo'
  138%     - oneTwo
  139%     - one_two
  140%     - 'One_Two'
  141%       Predefined self explanatory style identifiers
  142%     - style(+CapitaliseFirst, +CapitaliseRest, +Separator)
  143%       This generalises the above styles.  CapitaliseFirst defines
  144%       whether the first character of the first part must be a
  145%       capital, CapitaliseRest defines capitalization of the remaining
  146%       identifier parts and Separator is the character to place between
  147%       the parts.
  148
  149join_identifier_parts(Style, [First|Parts], Identifier) :-
  150    style(Style, CapFirst, CapRest, Sep),
  151    capitalise(CapFirst, First, H),
  152    maplist(capitalise(CapRest), Parts, T),
  153    atomic_list_concat([H|T], Sep, Identifier).
  154
  155%!  style(?Style, ?CapitaliseFirst, ?CapitaliseRest, ?Separator)
  156
  157style('OneTwo',  true,  true,  '').
  158style(oneTwo,    false, true,  '').
  159style(one_two,   false, false, '_').
  160style('One_Two', true,  true,  '_').
  161style(style(CFirst, CRest, Sep), CFirst, CRest, Sep).
  162
  163capitalise(false, X, X) :- !.
  164capitalise(true, X, Y) :-
  165    atom_codes(X, [H0|T]),
  166    code_type(H0, to_lower(H)),
  167    atom_codes(Y, [H|T])