%%
%% This is file `l3str-enc-utf32.def',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% l3str-convert.dtx  (with options: `utf32')
%% 
%% EXPERIMENTAL CODE
%% 
%% Do not distribute this file without also distributing the
%% source files specified above.
%% 
%% Do not distribute a modified version of this file.
%% 
%% File: l3str-convert.dtx Copyright (C) 2013 The LaTeX3 Project
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This file is part of the "l3experimental bundle" (The Work in LPPL)
%% and all files in that bundle must be distributed together.
%%
%% The released version of this bundle is available from CTAN.
%%
%% -----------------------------------------------------------------------
%%
%% The development version of the bundle can be found at
%%
%%    http://www.latex-project.org/svnroot/experimental/trunk/
%%
%% for those people who are interested.
%%
%%%%%%%%%%%
%% NOTE: %%
%%%%%%%%%%%
%%
%%   Snapshots taken from the repository represent work in progress and may
%%   not work or may contain conflicting material!  We therefore ask
%%   people _not_ to put them into distributions, archives, etc. without
%%   prior consultation with the LaTeX3 Project.
%%
%% -----------------------------------------------------------------------
\group_begin:
  \char_set_catcode_other:N \^^00
  \char_set_catcode_other:N \^^fe
  \char_set_catcode_other:N \^^ff
  \cs_new_protected_nopar:cpn { __str_convert_encode_utf32: }
    {
      \__str_convert_gmap_internal:N \__str_encode_utf_xxxii_be:n
      \tl_gput_left:Nx \g__str_result_tl { ^^00 ^^00 ^^fe ^^ff }
    }
  \cs_new_protected_nopar:cpn { __str_convert_encode_utf32be: }
    { \__str_convert_gmap_internal:N \__str_encode_utf_xxxii_be:n }
  \cs_new_protected_nopar:cpn { __str_convert_encode_utf32le: }
    { \__str_convert_gmap_internal:N \__str_encode_utf_xxxii_le:n }
  \cs_new:Npn \__str_encode_utf_xxxii_be:n #1
    {
      \exp_args:Nf \__str_encode_utf_xxxii_be_aux:nn
        { \int_div_truncate:nn {#1} { "100 } } {#1}
    }
  \cs_new:Npn \__str_encode_utf_xxxii_be_aux:nn #1#2
    {
      ^^00
      \__str_output_byte_pair_be:n {#1}
      \__str_output_byte:n { #2 - #1 * "100 }
    }
  \cs_new:Npn \__str_encode_utf_xxxii_le:n #1
    {
      \exp_args:Nf \__str_encode_utf_xxxii_le_aux:nn
        { \int_div_truncate:nn {#1} { "100 } } {#1}
    }
  \cs_new:Npn \__str_encode_utf_xxxii_le_aux:nn #1#2
    {
      \__str_output_byte:n { #2 - #1 * "100 }
      \__str_output_byte_pair_le:n {#1}
      ^^00
    }
  \flag_clear_new:n { str_overflow }
  \flag_clear_new:n { str_end }
  \__msg_kernel_new:nnnn { str } { utf32-decode }
    {
      Invalid~UTF-32~string: \exp_last_unbraced:Nf \use_none:n
      \__str_if_flag_times:nT { str_overflow } { ,~code~point~too~large }
      \__str_if_flag_times:nT { str_end }      { ,~truncated~string }
      .
    }
    {
      In~the~UTF-32~encoding,~every~Unicode~character~
      (in~the~range~[U+0000,~U+10FFFF])~is~encoded~as~4~bytes.
      \flag_if_raised:nT { str_overflow }
        {
          \\\\
          LaTeX~came~across~a~code~point~larger~than~1114111,~
          the~maximum~code~point~defined~by~Unicode.~
          Perhaps~the~string~was~not~encoded~in~the~UTF-32~encoding?
        }
      \flag_if_raised:nT { str_end }
        {
          \\\\
          The~length~of~the~string~is~not~a~multiple~of~4.~
          Perhaps~the~string~was~truncated?
        }
    }
  \cs_new_protected_nopar:cpn { __str_convert_decode_utf32be: }
    { \__str_decode_utf_xxxii:Nw 1 \g__str_result_tl \s__stop }
  \cs_new_protected_nopar:cpn { __str_convert_decode_utf32le: }
    { \__str_decode_utf_xxxii:Nw 2 \g__str_result_tl \s__stop }
  \cs_new_protected_nopar:cpn { __str_convert_decode_utf32: }
    {
      \exp_after:wN \__str_decode_utf_xxxii_bom:NNNN \g__str_result_tl
        \s__stop \s__stop \s__stop \s__stop \s__stop
    }
  \cs_new_protected:Npn \__str_decode_utf_xxxii_bom:NNNN #1#2#3#4
    {
      \str_if_eq_x:nnTF { #1#2#3#4 } { ^^ff ^^fe ^^00 ^^00 }
        { \__str_decode_utf_xxxii:Nw 2 }
        {
          \str_if_eq_x:nnTF { #1#2#3#4 } { ^^00 ^^00 ^^fe ^^ff }
            { \__str_decode_utf_xxxii:Nw 1 }
            { \__str_decode_utf_xxxii:Nw 1 #1#2#3#4 }
        }
    }
  \cs_new_protected:Npn \__str_decode_utf_xxxii:Nw #1#2 \s__stop
    {
      \flag_clear:n { str_overflow }
      \flag_clear:n { str_end }
      \flag_clear:n { str_error }
      \cs_set:Npn \__str_tmp:w ##1 ##2 { ` ## #1 }
      \tl_gset:Nx \g__str_result_tl
        {
          \exp_after:wN \__str_decode_utf_xxxii_loop:NNNN
            #2 \s__stop \s__stop \s__stop \s__stop
          \__prg_break_point:
        }
      \__str_if_flag_error:nnx { str_error } { utf32-decode } { }
    }
  \cs_new:Npn \__str_decode_utf_xxxii_loop:NNNN #1#2#3#4
    {
      \if_meaning:w \s__stop #4
        \exp_after:wN \__str_decode_utf_xxxii_end:w
      \fi:
      #1#2#3#4 \s__tl
      \if_int_compare:w \__str_tmp:w #1#4 > \c_zero
        \flag_raise:n { str_overflow }
        \flag_raise:n { str_error }
        \int_use:N \c__str_replacement_char_int
      \else:
        \if_int_compare:w \__str_tmp:w #2#3 > \c_sixteen
          \flag_raise:n { str_overflow }
          \flag_raise:n { str_error }
          \int_use:N \c__str_replacement_char_int
        \else:
          \int_eval:n
            { \__str_tmp:w #2#3*"10000 + \__str_tmp:w #3#2*"100 + \__str_tmp:w #4#1 }
        \fi:
      \fi:
      \s__tl
      \__str_decode_utf_xxxii_loop:NNNN
    }
  \cs_new:Npn \__str_decode_utf_xxxii_end:w #1 \s__stop
    {
      \tl_if_empty:nF {#1}
        {
          \flag_raise:n { str_end }
          \flag_raise:n { str_error }
          #1 \s__tl
          \int_use:N \c__str_replacement_char_int \s__tl
        }
      \__prg_break:
    }
\group_end:
%% 
%%
%% End of file `l3str-enc-utf32.def'.
