Skip to content

Instantly share code, notes, and snippets.

@h20y6m
Created June 18, 2023 14:28
Show Gist options
  • Save h20y6m/3069923eb4c739071de7063bd4c07313 to your computer and use it in GitHub Desktop.
Save h20y6m/3069923eb4c739071de7063bd4c07313 to your computer and use it in GitHub Desktop.
Patch for l3str-convert for (u)pLaTeX
\NeedsTeXFormat{pLaTeX2e}
\ProvidesExplPackage{pl3str-convert}{2023-06-05}{}
{Patch for l3str-convert for (u)pLaTeX}
% Check if pTeX is old.
% Needs pTeX p3.10.0 or latar.
\msg_new:nnn { pl3str-convert } { old-ptex }
{ Your~pTeX~is~too~old. }
\token_if_primitive:NF \toucs
{ \msg_error:nn { pl3str-convert } { old-ptex } }
% Check that the expl3 function definitions are what we expect.
\msg_new:nnn { pl3str-convert } { definition-mismatch }
{ Definition~of~function~`#1'~does~not~match. }
\cs_new:Npn \__plstr_original_decode_utf_viii_start:N #1
{
#1
\if_int_compare:w `#1 < "C0 \exp_stop_f:
\s__str
\if_int_compare:w `#1 < "80 \exp_stop_f:
\int_value:w `#1
\else:
\flag_raise:n { str_extra }
\flag_raise:n { str_error }
\int_use:N \c__str_replacement_char_int
\fi:
\else:
\exp_after:wN \__str_decode_utf_viii_continuation:wwN
\int_value:w \int_eval:n { `#1 - "C0 } \exp_after:wN
\fi:
\s__str
\__str_use_none_delimit_by_s_stop:w {"80} {"800} {"10000} {"110000} \s__str_stop
\__str_decode_utf_viii_start:N
}
\cs_if_eq:NNF
\__str_decode_utf_viii_start:N
\__plstr_original_decode_utf_viii_start:N
{
\msg_error:nnn { pl3str-convert } { definition-mismatch }
{ __str_decode_utf_viii_start:N }
}
% Make expl3's UTF-8 decoding function compatible with Japanese character tokens.
\cs_gset:Npn \__str_decode_utf_viii_start:N #1
{
#1
\__plstr_if_jachar:NTF #1
{ \__plstr_decode_utf_viii_start_jachar:N #1 }
{ \__plstr_decode_utf_viii_start_alchar:N #1 }
\__str_decode_utf_viii_start:N
}
\cs_new:Npn \__plstr_decode_utf_viii_start_alchar:N #1
{
\if_int_compare:w `#1 < "C0 \exp_stop_f:
\s__str
\if_int_compare:w `#1 < "80 \exp_stop_f:
\int_value:w `#1
\else:
\flag_raise:n { str_extra }
\flag_raise:n { str_error }
\int_use:N \c__str_replacement_char_int
\fi:
\else:
\exp_after:wN \__str_decode_utf_viii_continuation:wwN
\int_value:w \int_eval:n { `#1 - "C0 } \exp_after:wN
\fi:
\s__str
\__str_use_none_delimit_by_s_stop:w {"80} {"800} {"10000} {"110000} \s__str_stop
}
\cs_new:Npn \__plstr_decode_utf_viii_start_jachar:N #1
{
\s__str
\tex_toucs:D `#1
\s__str
}
% Tests if internal kanji encoding is unicode.
\prg_new_conditional:Npnn \__plstr_if_encoding_unicode: { p , T , F , TF }
{
\if_int_compare:w \tex_jis:D "2121 = "3000 \exp_stop_f:
\prg_return_true:
\else:
\prg_return_false:
\fi:
}
% Define Japanese character token whitch has specific kanji category code.
\group_begin:
\cs_set:Npn \__plstr_tmp:w #1#2
{
\tex_kansujichar:D 0 = \tex_jis:D "2121 \scan_stop:
\tex_kcatcode:D \tex_jis:D "2121 = #2 \scan_stop:
\exp_after:wN \cs_new_eq:NN \exp_after:wN #1
\tex_kansuji:D 0 \scan_stop:
}
\__plstr_tmp:w \c__plstr_kanji_token { 16 }
\__plstr_tmp:w \c__plstr_kana_token { 17 }
\__plstr_tmp:w \c__plstr_symbol_token { 18 }
\sys_if_engine_uptex:T
{ \__plstr_tmp:w \c__plstr_hangul_token { 19 } }
\group_end:
% Tests whether the token is Japanese character.
\sys_if_engine_uptex:TF
{
\prg_new_conditional:Npnn \__plstr_if_jachar:N #1 { p , T , F , TF }
{
\if_catcode:w \exp_not:N #1 \c__plstr_kanji_token
\prg_return_true:
\else:
\if_catcode:w \exp_not:N #1 \c__plstr_kana_token
\prg_return_true:
\else:
\if_catcode:w \exp_not:N #1 \c__plstr_symbol_token
\prg_return_true:
\else:
\if_catcode:w \exp_not:N #1 \c__plstr_hangul_token
\prg_return_true:
\else:
\prg_return_false:
\fi:
\fi:
\fi:
\fi:
}
}
{
\prg_new_conditional:Npnn \__plstr_if_jachar:N #1 { p , T , F , TF }
{
\if_catcode:w \exp_not:N #1 \c__plstr_kanji_token
\prg_return_true:
\else:
\if_catcode:w \exp_not:N #1 \c__plstr_kana_token
\prg_return_true:
\else:
\if_catcode:w \exp_not:N #1 \c__plstr_symbol_token
\prg_return_true:
\else:
\prg_return_false:
\fi:
\fi:
\fi:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment