%%
%% This is file `akshar.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% akshar.dtx  (with options: `package')
%% 
%% ---------------------------------------------------------------------------
%% Package:      akshar
%% Author:       Vu Van Dung
%% Description:  Support for syllables in the Devanagari script
%% Repository:   https://github.com/joulev/akshar
%% Bug tracker:  https://github.com/joulev/akshar/issues
%% License:      The LaTeX Project Public License v1.3c or later
%% ---------------------------------------------------------------------------
%% This work may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License, either version 1.3c of this license or (at
%% your option) any later version.
%% 
%% The latest version of this license is in
%% 
%%     http://www.latex-project.org/lppl.txt.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is Vu Van Dung.
%% 
%% This work consists of the files akshar.dtx
%%                                 akshar.ins
%% and the derived file            akshar.sty.
%% ---------------------------------------------------------------------------
%% 
\def\aksharPackageName{akshar}
\def\aksharPackageVersion{0.1}
\def\aksharPackageDate{2020/05/23}
\def\aksharPackageDescription{Support for syllables in the Devanagari script (JV)}
\RequirePackage{fontspec}
\ProvidesExplPackage {\aksharPackageName}
  {\aksharPackageDate} {\aksharPackageVersion} {\aksharPackageDescription}
\tl_const:Nn \c__akshar_joining_tl { ्}
\tl_const:Nn \c__akshar_diacritics_tl
  {
    ा,  ि,  ी,  ु,  ू,  े,  ै,  ो,  ौ,  ं,  ः,  ॢ,  ृ,  ॅ,  ँ,  ़,  ॆ,  ॊ,
    ॉ,  ॎ,  ॏ,  ॑,  ॒,  ॓,  ॔,  ॕ,  ॖ,  ॗ,  ॄ,  ऺ,  ऻ,  ꣠,  ꣡,  ꣢,  ꣣,  ꣤,  ꣥,
    ꣦,  ꣧,  ꣨,  ꣩,  ꣪,  ꣫,  ꣬,  ꣭,  ꣮,  ꣯,  ꣰,  ꣱,  ꣿ
  }
\bool_new:N \l__akshar_prev_joining_bool
\seq_new:N \l__akshar_char_seq
\tl_new:N \l__akshar_tmpa_tl
\tl_new:N \l__akshar_tmpb_tl
\seq_new:N \l__akshar_tmpa_seq
\seq_new:N \l__akshar_tmpb_seq
\seq_new:N \l__akshar_tmpc_seq
\seq_new:N \l__akshar_tmpd_seq
\seq_new:N \l__akshar_tmpe_seq
\int_new:N \l__akshar_tmpa_int
\int_new:N \l__akshar_tmpb_int
\msg_new:nnnn { akshar } { err_not_a_sequence_variable }
  { #1 ~ is ~ not ~ a ~ valid ~ LaTeX3 ~ sequence ~ variable. }
  {
    You ~ have ~ requested ~ me ~ to ~ assign ~ some ~ value ~ to ~
    the ~ control ~ sequence ~ #1, ~ but ~ it ~ is ~ not ~ a ~ valid ~
    sequence ~ variable. ~ Read ~ the ~ documentation ~ of ~ expl3 ~
    for ~ more ~ information. ~ Proceed ~ and ~ I ~ will ~ pretend ~
    that ~ #1 ~ is ~ a ~ local ~ sequence ~ variable ~ (beware ~ that ~
    unexpected ~ behaviours ~ may ~ occur).
  }
\msg_new:nnnn { akshar } { err_character_out_of_bound }
  { Character ~ index ~ out ~ of ~ bound. }
  {
    You ~ are ~ trying ~ to ~ get ~ the ~ #2 ~ character ~ of ~ the ~
    string ~ #1. ~ However ~ that ~ character ~ doesn't ~ exist. ~
    Make ~ sure ~ that ~ you ~ use ~ a ~ number ~ between ~ and ~ not ~
    including ~ 0 ~ and ~ #3, ~ so ~ that ~ I ~ can ~ return ~ a ~
    good ~ output. ~ Proceed ~ and ~ I ~ will ~ return ~
    \token_to_str:N \scan_stop:.
  }
\msg_new:nnnn { akshar } { err_string_empty }
  { The ~ input ~ string ~ is ~ empty. }
  {
    To ~ get ~ the ~ #1 ~ character ~ of ~ a ~ string, ~ that ~ string ~
    must ~ not ~ be ~ empty, ~ but ~ the ~ input ~ string ~ is ~ empty.
    Make ~ sure ~ the ~ string ~ contains ~ something, ~ or ~ proceed ~
    and ~ I ~ will ~ use ~ \token_to_str:N \scan_stop:.
  }
\prg_generate_conditional_variant:Nnn \tl_if_in:Nn { No } { TF }
\cs_generate_variant:Nn \seq_set_split:Nnn { Nxx }
\cs_generate_variant:Nn \msg_error:nnn { nnx }
\cs_generate_variant:Nn \msg_error:nnnnn { nnnxx }
\tl_const:Nx \c__akshar_str_g_tl { \tl_to_str:n {g} }
\tl_const:Nx \c__akshar_str_seq_tl { \tl_to_str:n {seq} }
\prg_new_conditional:Npnn \__akshar_var_if_global:N #1 { T, F, TF }
  {
    \bool_if:nTF
      { \exp_last_unbraced:Nf \use_iii:nnn { \cs_split_function:N #1 } }
      {
        \msg_error:nnx { akshar } { err_not_a_sequence_variable }
          { \token_to_str:N #1 }
        \prg_return_false:
      }
      {
        \seq_set_split:Nxx \l__akshar_tmpb_seq { \token_to_str:N _ }
          { \exp_last_unbraced:Nf \use_i:nnn { \cs_split_function:N #1 } }
        \seq_get_left:NN  \l__akshar_tmpb_seq \l__akshar_tmpa_tl
        \seq_get_right:NN \l__akshar_tmpb_seq \l__akshar_tmpb_tl
        \tl_if_eq:NNTF \c__akshar_str_seq_tl \l__akshar_tmpb_tl
          {
            \tl_if_eq:NNTF \c__akshar_str_g_tl \l__akshar_tmpa_tl
              { \prg_return_true: } { \prg_return_false: }
          }
          {
            \msg_error:nnx { akshar } { err_not_a_sequence_variable }
              { \token_to_str:N #1 }
            \prg_return_false:
          }
      }
  }
\cs_new:Npn \__akshar_int_append_ordinal:n #1
  {
    #1
    \int_case:nnF { #1 }
      {
        { 11 } { th }
        { 12 } { th }
        { 13 } { th }
        { -11 } { th }
        { -12 } { th }
        { -13 } { th }
      }
      {
        \int_compare:nNnTF { #1 } > { -1 }
          {
            \int_case:nnF { #1 - 10 * (#1 / 10) }
              {
                { 1 } { st }
                { 2 } { nd }
                { 3 } { rd }
              } { th }
          }
          {
            \int_case:nnF { (- #1) - 10 * ((- #1) / 10) }
              {
                { 1 } { st }
                { 2 } { nd }
                { 3 } { rd }
              } { th }
          }
      }
  }
\cs_new:Npn \akshar_convert:Nn #1 #2
  {
    \seq_clear:N \l__akshar_char_seq
    \bool_set_false:N \l__akshar_prev_joining_bool
    \tl_map_variable:NNn {#2} \l__akshar_map_tl
      {
        \tl_if_in:NoTF \c__akshar_diacritics_tl {\l__akshar_map_tl}
          {
            \seq_pop_right:NN \l__akshar_char_seq \l__akshar_tmpa_tl
            \seq_put_right:Nx \l__akshar_char_seq
              { \l__akshar_tmpa_tl \l__akshar_map_tl }
          }
          {
            \tl_if_eq:NNTF \l__akshar_map_tl \c__akshar_joining_tl
              {
                \seq_pop_right:NN \l__akshar_char_seq \l__akshar_tmpa_tl
                \seq_put_right:Nx \l__akshar_char_seq
                  { \l__akshar_tmpa_tl \l__akshar_map_tl }
                \bool_set_true:N \l__akshar_prev_joining_bool
              }
              {
                \bool_if:NTF \l__akshar_prev_joining_bool
                  {
                    \seq_pop_right:NN \l__akshar_char_seq \l__akshar_tmpa_tl
                    \seq_put_right:Nx \l__akshar_char_seq
                      { \l__akshar_tmpa_tl \l__akshar_map_tl }
                    \bool_set_false:N \l__akshar_prev_joining_bool
                  }
                  {
                    \seq_put_right:Nx
                      \l__akshar_char_seq { \l__akshar_map_tl }
                  }
              }
          }
      }
    \__akshar_var_if_global:NTF #1
      { \seq_gset_eq:NN #1 \l__akshar_char_seq }
      { \seq_set_eq:NN #1 \l__akshar_char_seq }
  }
\cs_generate_variant:Nn \akshar_convert:Nn { cn, Nx, cx }
\cs_new:Npn \__akshar_seq_push_seq:NN #1 #2
  { \seq_map_inline:Nn #2 { \seq_put_right:Nn #1 { ##1 } } }
\cs_new:Npn \__akshar_replace:NnnnN #1 #2 #3 #4 #5
  {
    \akshar_convert:Nn \l__akshar_tmpc_seq {#2}
    \akshar_convert:Nn \l__akshar_tmpd_seq {#3}
    \akshar_convert:Nn \l__akshar_tmpe_seq {#4}
    \seq_clear:N \l__akshar_tmpa_seq
    \seq_clear:N \l__akshar_tmpb_seq
    \int_set:Nn \l__akshar_tmpa_int { 1 }
    \int_set:Nn \l__akshar_tmpb_int { 0 }
    \seq_map_variable:NNn \l__akshar_tmpc_seq \l__akshar_map_tl
      {
        \int_compare:nNnTF { \l__akshar_tmpb_int } > { 0 }
          { \seq_put_right:NV \l__akshar_tmpb_seq \l__akshar_map_tl }
          {
            \int_compare:nNnTF
              {\l__akshar_tmpa_int} = {1 + \seq_count:N \l__akshar_tmpd_seq}
              {
                \bool_if:NT {#5}
                  { \int_incr:N \l__akshar_tmpb_int }
                \seq_clear:N \l__akshar_tmpb_seq
                \__akshar_seq_push_seq:NN
                  \l__akshar_tmpa_seq \l__akshar_tmpe_seq
                \int_set:Nn \l__akshar_tmpa_int { 1 }
                \tl_set:Nx \l__akshar_tmpa_tl
                  { \seq_item:Nn \l__akshar_tmpd_seq { 1 } }
                \tl_if_eq:NNTF \l__akshar_map_tl \l__akshar_tmpa_tl
                  {
                    \int_incr:N \l__akshar_tmpa_int
                    \seq_put_right:NV \l__akshar_tmpb_seq \l__akshar_map_tl
                  }
                  {
                    \seq_put_right:NV \l__akshar_tmpa_seq \l__akshar_map_tl
                  }
              }
              {
                \tl_set:Nx \l__akshar_tmpa_tl
                  {
                    \seq_item:Nn \l__akshar_tmpd_seq { \l__akshar_tmpa_int }
                  }
                \tl_if_eq:NNTF \l__akshar_map_tl \l__akshar_tmpa_tl
                  {
                    \int_incr:N \l__akshar_tmpa_int
                    \seq_put_right:NV \l__akshar_tmpb_seq \l__akshar_map_tl
                  }
                  {
                    \int_set:Nn \l__akshar_tmpa_int { 1 }
                    \__akshar_seq_push_seq:NN
                      \l__akshar_tmpa_seq \l__akshar_tmpb_seq
                    \seq_clear:N \l__akshar_tmpb_seq
                    \tl_set:Nx \l__akshar_tmpa_tl
                      { \seq_item:Nn \l__akshar_tmpd_seq { 1 } }
                    \tl_if_eq:NNTF \l__akshar_map_tl \l__akshar_tmpa_tl
                      {
                        \int_incr:N \l__akshar_tmpa_int
                        \seq_put_right:NV
                          \l__akshar_tmpb_seq \l__akshar_map_tl
                      }
                      {
                        \seq_put_right:NV
                          \l__akshar_tmpa_seq \l__akshar_map_tl
                      }
                  }
              }
          }
      }
    \__akshar_seq_push_seq:NN \l__akshar_tmpa_seq \l__akshar_tmpb_seq
    \__akshar_var_if_global:NTF #1
      { \seq_gset_eq:NN #1 \l__akshar_tmpa_seq }
      { \seq_set_eq:NN #1 \l__akshar_tmpa_seq }
  }
\NewExpandableDocumentCommand \aksharStrLen {m}
  {
    \akshar_convert:Nn \l__akshar_tmpa_seq {#1}
    \seq_count:N \l__akshar_tmpa_seq
  }
\NewExpandableDocumentCommand \aksharStrChar {mm}
  {
    \akshar_convert:Nn \l__akshar_tmpa_seq {#1}
    \bool_if:nTF
      {
        \int_compare_p:nNn {#2} > {0} &&
        \int_compare_p:nNn {#2} < {1 + \seq_count:N \l__akshar_tmpa_seq}
      }
      { \seq_item:Nn \l__akshar_tmpa_seq { #2 } }
      {
        \msg_error:nnnxx { akshar } { err_character_out_of_bound }
          { #1 } { \__akshar_int_append_ordinal:n { #2 } }
          { \int_eval:n { 1 + \seq_count:N \l__akshar_tmpa_seq } }
        \scan_stop:
      }
  }
\NewExpandableDocumentCommand \aksharStrHead {m}
  {
    \akshar_convert:Nn \l__akshar_tmpa_seq {#1}
    \int_compare:nNnTF { \seq_count:N \l__akshar_tmpa_seq } = {0}
      {
        \msg_error:nnn { akshar } { err_character_out_of_bound }
          { first }
        \scan_stop:
      }
      { \seq_item:Nn \l__akshar_tmpa_seq { 1 } }
  }
\NewExpandableDocumentCommand \aksharStrTail {m}
  {
    \akshar_convert:Nn \l__akshar_tmpa_seq {#1}
    \int_compare:nNnTF { \seq_count:N \l__akshar_tmpa_seq } = {0}
      {
        \msg_error:nnn { akshar } { err_character_out_of_bound }
          { last }
        \scan_stop:
      }
      { \seq_item:Nn \l__akshar_tmpa_seq {\seq_count:N \l__akshar_tmpa_seq} }
  }
\NewExpandableDocumentCommand \aksharStrReplace {smmm}
  {
    \IfBooleanTF {#1}
      {
        \__akshar_replace:NnnnN \l__akshar_tmpa_seq
          {#2} {#3} {#4} \c_true_bool
      }
      {
        \__akshar_replace:NnnnN \l__akshar_tmpa_seq
          {#2} {#3} {#4} \c_false_bool
      }
    \seq_use:Nn \l__akshar_tmpa_seq {}
  }
\NewExpandableDocumentCommand \aksharStrRemove {smm}
  {
    \IfBooleanTF {#1}
      {
        \__akshar_replace:NnnnN \l__akshar_tmpa_seq
          {#2} {#3} {} \c_true_bool
      }
      {
        \__akshar_replace:NnnnN \l__akshar_tmpa_seq
          {#2} {#3} {} \c_false_bool
      }
    \seq_use:Nn \l__akshar_tmpa_seq {}
  }
\endinput
%%
%% End of file `akshar.sty'.
