1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2015, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(trim_utils,
   36          [left_trim/2,
   37           right_trim/2,
   38           trim/2,
   39           atom_left_trim/2,
   40           atom_right_trim/2,
   41           atom_trim/2,
   42           string_left_trim/2,
   43           string_right_trim/2,
   44           string_trim/2
   45           ]).   46
   47sub_atomic(atom,   T, B, L, A, S) :- sub_atom(  T, B, L, A, S).
   48sub_atomic(string, T, B, L, A, S) :- sub_string(T, B, L, A, S).
   49
   50/* % Avoiding usage of pcre due to crashes in big repositories:
   51
   52:- use_module(library(pcre)).
   53
   54left_trim(Type, Atomic, LeftTrim) :-
   55    % Can get valid spaces using char_type(A, space), except special ones
   56    re_replace("^[ \t\n\v\f\r]+", "", Atomic, LeftTrim, [capture_type(Type)]).
   57
   58right_trim(Type, Atomic, RightTrim) :-
   59    re_replace("[ \t\n\v\f\r]+$", "", Atomic, RightTrim, [capture_type(Type)]).
   60*/
   61
   62left_trim(Type, Atomic, LeftTrim) :-
   63    ( sub_atomic(Type, Atomic, B, 1, _, C),
   64      \+ char_type(C, space)
   65    ->true
   66    ; string_length(Atomic, B)
   67    ),
   68    sub_atomic(Type, Atomic, B, _, 0, LeftTrim).
   69
   70right_trim(Type, Atomic, RightTrim) :-
   71    ( string_length(Atomic, N),
   72      between(0, N, X),
   73      sub_atomic(Type, Atomic, B, 1, X, C),
   74      ( \+ char_type(C, space)
   75      ->succ(B, E)
   76      ; B = 0,
   77        E = 0
   78      )
   79    ->true
   80    ; E = 0
   81    ),
   82    sub_atomic(Type, Atomic, 0, E, _, RightTrim).
   83
   84trim(Type, Atomic, Trim) :-
   85    left_trim(Type, Atomic, LTrim),
   86    right_trim(Type, LTrim, Trim).
   87
   88atom_left_trim(Atom, LeftTrim) :-
   89    left_trim(atom, Atom, LeftTrim).
   90
   91atom_right_trim(Atom, RightTrim) :-
   92    right_trim(atom, Atom, RightTrim).
   93
   94atom_trim(Atom, Trim) :-
   95    trim(atom, Atom, Trim).
   96
   97string_left_trim(String, LeftTrim) :-
   98    left_trim(string, String, LeftTrim).
   99
  100string_right_trim(String, RightTrim) :-
  101    right_trim(string, String, RightTrim).
  102
  103string_trim(String, Trim) :-
  104    trim(string, String, Trim).
  105
  106left_trim(Codes, LTrim) :-
  107    string_codes(String, Codes),
  108    string_left_trim(String, STrim),
  109    string_codes(STrim, LTrim).
  110
  111right_trim(Codes, RTrim) :-
  112    string_codes(String, Codes),
  113    string_right_trim(String, STrim),
  114    string_codes(STrim, RTrim).
  115
  116trim(Codes, Trim) :-
  117    string_codes(String, Codes),
  118    string_trim(String, STrim),
  119    string_codes(STrim, Trim)