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:- use_module(library(apply)).   43
   44/** <module> Operations on atoms
   45
   46This library provides operations  on  atoms   that  are  not  covered by
   47builtin predicates. The current implementation is   just a start, making
   48code developed in _xpce_ and duplicated in various projects reusable.
   49*/
   50
   51
   52                 /*******************************
   53                 *      RESTYLE IDENTIFIERS     *
   54                 *******************************/
   55
   56%!  restyle_identifier(+Style, +In, -Out) is det.
   57%
   58%   Restyle an identifier by extracting the alnum substrings and
   59%   joining them together according to Style.
   60%
   61%   @arg Style is one of `'OneTwo'`, `oneTwo`, `one_two`, `'One_Two'` or
   62%   a term style(CapitaliseFirst, CapitaliseRest, Separator).
   63
   64%   @see join_identifier_parts/3.
   65
   66restyle_identifier(Style, In, Out) :-
   67    identifier_parts(In, Parts),
   68    join_identifier_parts(Style, Parts, Out).
   69
   70
   71%!  identifier_parts(+Identifier, -Parts) is det.
   72%
   73%   Parts is a list of atoms  that   make  up  Identifier. The parts
   74%   found are turned into lowercase, unless   all its characters are
   75%   uppercase.  E.g.,
   76%
   77%       ?- identifier_parts('sourceCodeURI', X).
   78%       X = [source, code, 'URI'].
   79
   80identifier_parts(';', [';']) :- !.
   81identifier_parts('|', ['|']) :- !.
   82identifier_parts('!', ['!']) :- !.
   83identifier_parts(',', [',']) :- !.
   84identifier_parts(Name, Parts) :-
   85    atom_codes(Name, Codes),
   86    (   phrase(identifier_parts(Parts), Codes)
   87    ->  true
   88    ;   maplist(is_symbol_code, Codes)
   89    ->  Parts = [Name]
   90    ).
   91
   92is_symbol_code(Code) :-
   93    code_type(Code, prolog_symbol).
   94
   95identifier_parts([H|T]) -->
   96    identifier_part(H),
   97    !,
   98    identifier_parts(T).
   99identifier_parts([]) --> [].
  100
  101identifier_part(H) -->
  102    string(Codes, Tail),
  103    sep(Tail),
  104    !,
  105    { Codes = [_|_],
  106      atom_codes(H0, Codes),
  107      (   maplist(is_upper, Codes)
  108      ->  H = H0
  109      ;   downcase_atom(H0, H)
  110      )
  111    }.
  112
  113string(T,T) --> [].
  114string([H|T], L) --> [H], string(T, L).
  115
  116sep([]) --> sep_char, !, sep_chars.
  117sep([T]), [N] -->
  118    [T,N],
  119    { code_type(T, lower),
  120      code_type(N, upper)
  121    }.
  122sep([],[],[]).
  123
  124sep_char -->
  125    [H],
  126    { \+ code_type(H, alnum) }.
  127
  128sep_chars --> sep_char, !, sep_chars.
  129sep_chars --> [].
  130
  131%!  join_identifier_parts(+Style, +Parts, -Identifier)
  132%
  133%   Join parts of an identifier according to Style. Style is one of:
  134%
  135%     - 'OneTwo'
  136%     - oneTwo
  137%     - one_two
  138%     - 'One_Two'
  139%       Predefined self explanatory style identifiers
  140%     - style(+CapitaliseFirst, +CapitaliseRest, +Separator)
  141%       This generalises the above styles.  CapitaliseFirst defines
  142%       whether the first character of the first part must be a
  143%       capital, CapitaliseRest defines capitalization of the remaining
  144%       identifier parts and Separator is the character to place between
  145%       the parts.
  146
  147join_identifier_parts(Style, [First|Parts], Identifier) :-
  148    style(Style, CapFirst, CapRest, Sep),
  149    capitalise(CapFirst, First, H),
  150    maplist(capitalise(CapRest), Parts, T),
  151    atomic_list_concat([H|T], Sep, Identifier).
  152
  153%!  style(?Style, ?CapitaliseFirst, ?CapitaliseRest, ?Separator)
  154
  155style('OneTwo',  true,  true,  '').
  156style(oneTwo,    false, true,  '').
  157style(one_two,   false, false, '_').
  158style('One_Two', true,  true,  '_').
  159style(style(CFirst, CRest, Sep), CFirst, CRest, Sep).
  160
  161capitalise(false, X, X) :- !.
  162capitalise(true, X, Y) :-
  163    atom_codes(X, [H0|T]),
  164    code_type(H0, to_lower(H)),
  165    atom_codes(Y, [H|T])