%%
%% This is file `l3str-format.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% l3str-format.dtx  (with options: `package')
%% 
%% Copyright (C) 2011-2022 The LaTeX 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.
%% 
%% File: l3str-format.dtx
\RequirePackage{expl3}
\ProvidesExplPackage{l3str-format}{2022-04-20}{}
  {L3 Experimental string formatting}
\cs_generate_variant:Nn \use:nn { nf }
\cs_generate_variant:Nn \use:nnn { fnf }
\cs_generate_variant:Nn \tl_to_str:n { f }
\prg_new_conditional:Npnn \__str_format_if_digit:N #1 { TF }
  {
    \if_int_compare:w 9 < 1 #1 \exp_stop_f:
      \prg_return_true: \else: \prg_return_false: \fi:
  }
\cs_new:Npn \__str_format_put:nw #1 #2 \s__str_stop { #2 \s__str_stop #1 }
\cs_generate_variant:Nn \__str_format_put:nw { o , f }
\prg_new_conditional:Npnn \__str_format_if_in:nN #1#2 { TF }
  {
    \__str_format_if_in_aux:NN #2 #1
      { #2 \prg_return_false: \exp_after:wN \prg_break: \else: }
    \prg_break_point:
  }
\cs_new:Npn \__str_format_if_in_aux:NN #1#2
  {
    \if_charcode:w #1 #2
      \prg_return_true:
      \exp_after:wN \prg_break:
    \fi:
    \__str_format_if_in_aux:NN #1
  }
\cs_new:Npn \__str_format_parse:n #1
  {
    \exp_last_unbraced:Nf \__str_format_parse_auxi:NN
      { \__kernel_str_to_other:n {#1} } \s__str_stop \s__str_stop {#1}
  }
\cs_new:Npx \__str_format_parse_auxi:NN #1#2
  {
    \exp_not:N \__str_format_if_in:nNTF { < > = ^ } #2
      { \exp_not:N \__str_format_parse_auxiii:nN { #1 #2 } }
      {
        \exp_not:N \__str_format_parse_auxii:nN
          { \c_catcode_other_space_tl } #1 #2
      }
  }
\cs_new:Npn \__str_format_parse_auxii:nN #1#2
  {
    \__str_format_if_in:nNTF { < > = ^ } #2
      { \__str_format_parse_auxiii:nN { #1 #2 } }
      { \__str_format_parse_auxiii:nN { #1 ? } #2 }
  }
\cs_new:Npx \__str_format_parse_auxiii:nN #1#2
  {
    \exp_not:N \__str_format_if_in:nNTF
      { + - \c_catcode_other_space_tl }
      #2
      { \exp_not:N \__str_format_parse_auxiv:nwN { #1 #2 } ; }
      { \exp_not:N \__str_format_parse_auxiv:nwN { #1 ? } ; #2 }
  }
\cs_new:Npn \__str_format_parse_auxiv:nwN #1#2; #3
  {
    \__str_format_if_digit:NTF #3
      { \__str_format_parse_auxiv:nwN {#1} #2 #3 ; }
      { \__str_format_parse_auxv:nN { #1 {#2} } #3 }
  }
\cs_new:Npn \__str_format_parse_auxv:nN #1#2
  {
    \token_if_eq_charcode:NNTF . #2
      { \__str_format_parse_auxvi:nwN {#1} 0 ; }
      { \__str_format_parse_auxvii:nN { #1 { } } #2 }
  }
\cs_new:Npn \__str_format_parse_auxvi:nwN #1#2; #3
  {
    \__str_format_if_digit:NTF #3
      { \__str_format_parse_auxvi:nwN {#1} #2 #3 ; }
      { \__str_format_parse_auxvii:nN { #1 {#2} } #3 }
  }
\cs_new:Npn \__str_format_parse_auxvii:nN #1#2
  {
    \token_if_eq_meaning:NNTF \s__str_stop #2
      { \__str_format_parse_end:nwn { #1 ? } #2 }
      { \__str_format_parse_end:nwn { #1 #2 } }
  }
\cs_new:Npn \__str_format_parse_end:nwn #1 #2 \s__str_stop \s__str_stop #3
  {
    \tl_if_empty:nF {#2}
      { \msg_expandable_error:nnn { str } { invalid-format } {#3} }
    #1
  }
\cs_new:cpn { __str_format_align_<:nnnN } #1#2#3#4
  {
    \use:nf { #2 #1 }
      {
        \prg_replicate:nn
          { \int_max:nn { #3 - \__str_count:n { #2 #1 } } { 0 } }
          {#4}
      }
  }
\cs_new:cpn { __str_format_align_>:nnnN } #1#2#3#4
  {
    \prg_replicate:nn
      { \int_max:nn { #3 - \__str_count:n { #2 #1 } } { 0 } }
      {#4}
    #2 #1
  }
\cs_new:cpn { __str_format_align_^:nnnN } #1#2#3#4
  {
    \use:fnf
      {
        \prg_replicate:nn
          {
            \int_max:nn { 0 }
              { #3 - \__str_count:n { #2 #1 } - 1 }
            / 2
          }
          {#4}
      }
      { #2 #1 }
      {
        \prg_replicate:nn
          {
            \int_max:nn { 0 }
              { #3 - \__str_count:n { #2 #1 } }
            / 2
          }
          {#4}
      }
  }
\cs_new:cpn { __str_format_align_=:nnnN } #1#2#3#4
  {
    \use:nf {#2}
      {
        \prg_replicate:nn
          { \int_max:nn { #3 - \__str_count:n { #2 #1 } } { 0 } }
          {#4}
      }
    #1
  }
\cs_new:Npn \tl_format:Nn { \exp_args:No \tl_format:nn }
\cs_generate_variant:Nn \tl_format:Nn { c }
\cs_new:Npn \tl_format:nn #1#2
  {
    \tl_to_str:f
      {
        \exp_last_unbraced:Nf \__str_format_tl:NNNnnNn
          { \__str_format_parse:n {#2} }
          {#1}
      }
  }
\cs_new:Npn \__str_format_tl:NNNnnNn #1#2#3#4#5#6
  {
    \token_if_eq_charcode:NNTF #2 =
      {
        \msg_expandable_error:nnnn
          { str } { invalid-align-format } {#2} {tl}
        \__str_format_put:nw { #1 < }
      }
      {
        \token_if_eq_charcode:NNTF #2 ?
          { \__str_format_put:nw { #1 < } }
          { \__str_format_put:nw { #1 #2 } }
      }
    \token_if_eq_charcode:NNF #3 ?
      {
        \msg_expandable_error:nnnn
          { str } { invalid-sign-format } {#3} {tl}
      }
    \__str_format_put:nw { {#4} }
    \tl_if_empty:nTF {#5}
      { \__str_format_put:nw { \__str_range:nnn { {1} {-1} } } }
      { \__str_format_put:nw { \__str_range:nnn { {1} {#5} } } }
    \token_if_eq_charcode:NNF #6 s
      {
        \token_if_eq_charcode:NNF #6 ?
          {
            \msg_expandable_error:nnnn
              { str } { invalid-style-format } {#6} {tl}
          }
      }
    \__str_format_tl_s:NNnnNNn
    \s__str_stop
  }
\cs_new:Npn \__str_format_tl_s:NNnnNNn #1#2#3#4#5#6#7
  {
    \exp_args:Nc \exp_args:Nf
      { __str_format_align_#6:nnnN }
      { \exp_args:Nf #2 { \__kernel_str_to_other:n {#7} } #3 }
      { }
      {#4} #5
  }
\cs_new:Npn \seq_format:Nn #1#2
  {
    \tl_to_str:f
      {
        \__str_format_seq:ff
          { \exp_after:wN \use_i:nn \exp_after:wN \exp_stop_f: #1 }
          { \__str_format_parse:n {#2} }
      }
  }
\cs_generate_variant:Nn \seq_format:Nn { c }
\cs_new:Npn \__str_format_seq:nn #1#2
  {
    \__str_format_seq_loop:nnNn { } {#2}
      #1
      { ? \__str_format_seq_end:w } { }
  }
\cs_generate_variant:Nn \__str_format_seq:nn { ff }
\cs_new:Npn \__str_format_seq_loop:nnNn #1#2#3#4
  {
    \use_none:n #3
    \exp_args:Nf \__str_format_seq_loop:nnNn
      { \use:nf {#1} { \__str_format_tl:NNNnnNn #2 {#4} } }
      {#2}
  }
\cs_new:Npn \__str_format_seq_end:w #1#2#3#4 { \use_ii:nnn #3 }
\cs_new:Npn \int_format:nn #1
  { \exp_args:Nf \__str_format_int:nn { \int_eval:n {#1} } }
\cs_new:Npn \__str_format_int:nn #1#2
  {
    \tl_to_str:f
      {
        \exp_last_unbraced:Nf \__str_format_int:NNNnnNn
          { \__str_format_parse:n {#2} }
          {#1}
      }
  }
\cs_new:Npn \__str_format_int:NNNnnNn #1#2#3#4#5#6#7
  {
    \token_if_eq_charcode:NNTF #2 ?
      { \__str_format_put:nw { #1 > } }
      { \__str_format_put:nw { #1 #2 } }
    \int_compare:nNnTF {#7} < 0
      { \__str_format_put:nw { - } }
      {
        \str_case:nnF {#3}
          {
            { ~ } { \__str_format_put:ow { \c_catcode_other_space_tl } }
            { + } { \__str_format_put:nw { + } }
          }
          { \__str_format_put:nw { { } } }
      }
    \__str_format_put:nw { {#4} }
    \tl_if_empty:nF {#5}
      {
        \msg_expandable_error:nnnn
          { str } { invalid-precision-format } {#5} {int}
      }
    \str_case:nnF {#6}
      {
        { ? } { \__str_format_int:NwnnNNn \use:n }
        { d } { \__str_format_int:NwnnNNn \use:n }
        { b } { \__str_format_int:NwnnNNn \int_to_bin:n }
        { o } { \__str_format_int:NwnnNNn \int_to_oct:n }
        { X } { \__str_format_int:NwnnNNn \int_to_Hex:n }
      }
      {
        \msg_expandable_error:nnnn
          { str } { invalid-style-format } {#6} { int }
        \__str_format_int:NwnnNNn \use:n
      }
    \s__str_stop {#7}
  }
\cs_new:Npn \__str_format_int:NwnnNNn #1#2 \s__str_stop #3#4#5#6#7
  {
    \exp_args:Nc \exp_args:Nf
      { __str_format_align_#6:nnnN }
      { #1 { \int_abs:n {#7} } }
      {#4}
      {#3} #5
  }
\cs_new:Npn \fp_format:nn #1
  { \exp_args:Nf \__str_format_fp:nn { \fp_to_tl:n {#1} } }
\cs_new:Npn \__str_format_fp:nn #1#2
  {
    \tl_to_str:f
      {
        \exp_last_unbraced:Nf \__str_format_fp:NNNnnNn
          { \__str_format_parse:n {#2} }
          {#1}
      }
  }
\cs_new:Npn \__str_format_fp:NNNnnNn
    #1#2#3#4#5#6#7
  {
    \token_if_eq_charcode:NNTF #2 ?
      { \__str_format_put:nw { #1 > } }
      { \__str_format_put:nw { #1 #2 } }
    \tl_if_head_eq_charcode:nNTF {#7} -
      { \__str_format_put:nw { - } }
      {
        \str_case:nnF {#3}
          {
            { ~ } { \__str_format_put:ow { \c_catcode_other_space_tl } }
            { + } { \__str_format_put:nw { + } }
          }
          { \__str_format_put:nw { { } } }
      }
    \__str_format_put:nw { {#4} }
    \tl_if_empty:nTF {#5}
      {
        \token_if_eq_meaning:NNTF #6 ?
          { \__str_format_put:nw { {  } } }
          { \__str_format_put:nw { { 6} } }
      }
      { \__str_format_put:nw { {#5} } }
    \str_case:nnF {#6}
      {
        { e } { \__str_format_fp:wnnnNNn \__str_format_fp_e:nn }
        { f } { \__str_format_fp:wnnnNNn \__str_format_fp_f:nn }
        { g } { \__str_format_fp:wnnnNNn \__str_format_fp_g:nn }
        { ? } { \__str_format_fp:wnnnNNn \__str_format_fp_g:nn }
      }
      {
        \msg_expandable_error:nnnn
          { str } { invalid-style-format } {#6} { fp }
        \__str_format_fp:wnnnNNn \__str_format_fp_g:nn
      }
    \s__str_stop {#7}
  }
\cs_new:Npn \__str_format_fp:wnnnNNn
    #1 \s__str_stop #2 #3 #4 #5#6 #7
  {
    \exp_args:Nc \exp_args:Nf
      { __str_format_align_#6:nnnN }
      { #1 {#7} {#2} }
      {#4}
      {#3} #5
  }
\cs_new:Npn \__str_format_fp_round:nn #1 #2
  { \fp_to_tl:n { abs ( round ( #1 , #2 - logb(#1) - 1 ) ) } }
\cs_new:Npn \__str_format_fp_e:nn #1#2
  {
    \exp_args:Nf \__str_format_fp_e_aux:nn
      { \__str_format_fp_round:nn {#1} { #2 + 1 } }
      {#2}
  }
\cs_new:Npn \__str_format_fp_e_aux:nn #1#2
  {
    \str_case:nnF {#1}
      {
        {  inf } { inf }
        {  nan } { nan }
      }
      {
        \exp_last_unbraced:Nf \__str_format_fp_e_aux:wwn
          { \fp_to_scientific:n {#1} } ;
          {#2}
      }
  }
\cs_new:Npn \__str_format_fp_e_aux:wwn #1 . #2 e #3 ; #4
  {
    \__str_format_put:nw { e #3 }
    \int_compare:nNnTF {#4} < \c__fp_prec_int
      {
        \__str_format_put:fw { \str_range:nnn { #2 } { 1 } { #4 } }
        \__str_format_put:nw { #1 . }
      }
      {
        \__str_format_put:fw
          { \prg_replicate:nn { #4 - \c__fp_prec_int + 1 } { 0 } }
        \__str_format_put:nw { #1 . #2 }
      }
    \use_none:n \s__str_stop
  }
\cs_new:Npn \__str_format_fp_f:nn #1#2
  {
    \exp_args:Nf \__str_format_fp_f_aux:nn
      { \fp_to_tl:n { abs ( round ( #1 , #2 ) ) } }
      {#2}
  }
\cs_new:Npn \__str_format_fp_f_aux:nn #1#2
  {
    \str_case:nnF {#1}
      {
        { inf } { inf }
        { nan } { nan }
      }
      {
        \exp_last_unbraced:Nf \__str_format_fp_f_aux:wwwn
          { \fp_to_decimal:n {#1} } . . ; {#2}
      }
  }
\cs_new:Npn \__str_format_fp_f_aux:wwwn #1 . #2 . #3 ; #4
  {
    \use:nf { #1 . #2 }
      { \prg_replicate:nn { #4 - \__str_count:n {#2} } {0} }
  }
\cs_new:Npn \__str_format_fp_g:nn #1#2
  {
    \tl_if_empty:nTF {#2} { \fp_to_tl:n { abs(#1) } }
      {
        \exp_args:Nf \__str_format_fp_g_aux:nn
          { \__str_format_fp_round:nn {#1} { \int_max:nn {1} {#2} } }
          { \int_max:nn {1} {#2} }
      }
  }
\cs_new:Npn \__str_format_fp_g_aux:nn #1#2
  {
    \str_case:nnF {#1}
      {
        { 0 } { 0 }
        { inf } { inf }
        { nan } { nan }
      }
      {
        \fp_compare:nTF { 1e-4 <= #1 < 1e#2 }
          { \fp_to_decimal:n {#1} }
          {
            \exp_last_unbraced:Nf \__str_format_fp_trim:w
              { \fp_to_scientific:n {#1} }
          }
      }
  }
\cs_new:Npn \__str_format_fp_trim:w #1 e
  {
    \__str_format_fp_trim_loop:w #1
      ; \__str_format_fp_trim_loop:w 0; \__str_format_fp_trim_dot:w .; \s__str_stop e
  }
\cs_new:Npn \__str_format_fp_trim_loop:w #1 0; #2 { #2 #1 ; #2 }
\cs_new:Npn \__str_format_fp_trim_dot:w #1 .; { \__str_format_fp_trim_end:w #1 ; }
\cs_new:Npn \__str_format_fp_trim_end:w #1 ; #2 \s__str_stop { #1 }
\msg_new:nnn { str } { invalid-format }
  { Invalid~format~'#1'. }
\msg_new:nnn { str } { invalid-align-format }
  { Invalid~alignment~'#1'~for~type~'#2'. }
\msg_new:nnn { str } { invalid-sign-format }
  { Invalid~sign~'#1'~for~type~'#2'. }
\msg_new:nnn { str } { invalid-precision-format }
  { Invalid~precision~'#1'~for~type~'#2'. }
\msg_new:nnn { str } { invalid-style-format }
  { Invalid~style~'#1'~for~type~'#2'. }
\prop_gput:Nnn \g_msg_module_name_prop { str } { LaTeX3 }
\prop_gput:Nnn \g_msg_module_type_prop { str } { }
%% 
%%
%% End of file `l3str-format.sty'.
