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)  2006-2011, University of Amsterdam
    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(utf8,
   36          [ utf8_codes//1               % ?String
   37          ]).   38
   39/** <module> UTF-8 encoding/decoding on lists of character codes.
   40*/
   41
   42%!  utf8_codes(?Codes)// is det.
   43%
   44%   DCG translating between  a  Unicode   code-list  and  its  UTF-8
   45%   encoded  byte-string.  The  DCG  works   two  ways.  Encoding  a
   46%   code-list to a UTF-8 byte string is achieved using
   47%
   48%           phrase(utf8_codes(Codes), UTF8)
   49%
   50%   The  algorithm  is  a  close  copy    of  the  C-algorithm  used
   51%   internally and defined in src/pl-utf8.c
   52%
   53%   NOTE: in many  cases  you  can   avoid  this  library  and leave
   54%   encoding and decoding to I/O streams. If   only part of the data
   55%   is to be encoded the  encoding  of   a  stream  can  be switched
   56%   temporary using set_stream(Stream, encoding(utf8))
   57%
   58%   @see set_stream/2.
   59
   60utf8_codes([H|T]) -->
   61    utf8_code(H),
   62    !,
   63    utf8_codes(T).
   64utf8_codes([]) -->
   65    [].
   66
   67utf8_code(C) -->
   68    [C0],
   69    { nonvar(C0) },                % decoding version
   70    !,
   71    (   {C0 < 0x80}
   72    ->  {C = C0}
   73    ;   {C0/\0xe0 =:= 0xc0}
   74    ->  utf8_cont(C1, 0),
   75        {C is (C0/\0x1f)<<6\/C1}
   76    ;   {C0/\0xf0 =:= 0xe0}
   77    ->  utf8_cont(C1, 6),
   78        utf8_cont(C2, 0),
   79        {C is ((C0/\0xf)<<12)\/C1\/C2}
   80    ;   {C0/\0xf8 =:= 0xf0}
   81    ->  utf8_cont(C1, 12),
   82        utf8_cont(C2, 6),
   83        utf8_cont(C3, 0),
   84        {C is ((C0/\0x7)<<18)\/C1\/C2\/C3}
   85    ;   {C0/\0xfc =:= 0xf8}
   86    ->  utf8_cont(C1, 18),
   87        utf8_cont(C2, 12),
   88        utf8_cont(C3, 6),
   89        utf8_cont(C4, 0),
   90        {C is ((C0/\0x3)<<24)\/C1\/C2\/C3\/C4}
   91    ;   {C0/\0xfe =:= 0xfc}
   92    ->  utf8_cont(C1, 24),
   93        utf8_cont(C2, 18),
   94        utf8_cont(C3, 12),
   95        utf8_cont(C4, 6),
   96        utf8_cont(C5, 0),
   97        {C is ((C0/\0x1)<<30)\/C1\/C2\/C3\/C4\/C5}
   98    ).
   99utf8_code(C) -->
  100    { nonvar(C) },                 % encoding version
  101    !,
  102    (   { C < 0x80 }
  103    ->  [C]
  104    ;   { C < 0x800 }
  105    ->  { C0 is 0xc0\/((C>>6)/\0x1f),
  106          C1 is 0x80\/(C/\0x3f)
  107        },
  108        [C0,C1]
  109    ;   { C < 0x10000 }
  110    ->  { C0 is 0xe0\/((C>>12)/\0x0f),
  111          C1 is 0x80\/((C>>6)/\0x3f),
  112          C2 is 0x80\/(C/\0x3f)
  113        },
  114        [C0,C1,C2]
  115    ;   { C < 0x200000 }
  116    ->  { C0 is 0xf0\/((C>>18)/\0x07),
  117          C1 is 0x80\/((C>>12)/\0x3f),
  118          C2 is 0x80\/((C>>6)/\0x3f),
  119          C3 is 0x80\/(C/\0x3f)
  120        },
  121        [C0,C1,C2,C3]
  122    ;   { C < 0x4000000 }
  123    ->  { C0 is 0xf8\/((C>>24)/\0x03),
  124          C1 is 0x80\/((C>>18)/\0x3f),
  125          C2 is 0x80\/((C>>12)/\0x3f),
  126          C3 is 0x80\/((C>>6)/\0x3f),
  127          C4 is 0x80\/(C/\0x3f)
  128        },
  129        [C0,C1,C2,C3,C4]
  130    ;   { C < 0x80000000 }
  131    ->  { C0 is 0xfc\/((C>>30)/\0x01),
  132          C1 is 0x80\/((C>>24)/\0x3f),
  133          C2 is 0x80\/((C>>18)/\0x3f),
  134          C3 is 0x80\/((C>>12)/\0x3f),
  135          C4 is 0x80\/((C>>6)/\0x3f),
  136          C5 is 0x80\/(C/\0x3f)
  137        },
  138        [C0,C1,C2,C3,C4,C5]
  139    ).
  140
  141utf8_cont(Val, Shift) -->
  142    [C],
  143    { C/\0xc0 =:= 0x80,
  144      Val is (C/\0x3f)<<Shift
  145    }