%%
%% This is file 'upkcat.sty'.
%% 
%% Copyright (c) 2008-2023 Takayuki YATO (aka. "ZR")
%%   GitHub:   https://github.com/zr-tex8r
%%   Twitter:  @zr_tex8r
%%
%% This package is distributed under the MIT License.
%%

% Note: This package supports both upLaTeX and plain upTeX.

%% avoid multiple loading
\csname\if11upkcatstyLoaded\fi\endcsname
%% code guards
\edef\x{\catcode32=\the\catcode32%
\catcode43=\the\catcode43\catcode45=\the\catcode45%
\catcode46=\the\catcode46\catcode47=\the\catcode47%
\catcode60=\the\catcode60\catcode63=\the\catcode63%
\catcode64=\the\catcode64\catcode96=\the\catcode96%
\endlinechar=\the\endlinechar\relax}
\catcode32=10\relax\endlinechar=13\relax
\catcode43=12 \catcode45=12 % <+><->
\catcode46=12 \catcode47=12 % <.></>
\catcode60=12 \catcode63=12 % <<><?>
\catcode64=11 \catcode96=12 % <@><`>
\edef\pxkq@restore@codes{\x\let\noexpand\pxkq@restore@codes\relax}
%% check for LaTeX
\ifx\RequirePackage\@undefined %<*!LaTeX>
  \long\def\x#1\ProvidesPackage#2[#3]{\immediate\write-1{Package: #2 #3}}
  \expandafter\x
\else                          %<*LaTeX>
  \RequirePackage{ifuptex}[2008/03/14]\RequireNativeupTeX
\fi                            %</LaTeX>

%% package declaration
\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{upkcat}[2023/02/25 v1.4 safe kcatcode access]

%--------------------------------------- main

%%<*> \getkcatcode{<jachar>}
\def\getkcatcode#1{\pxkq@getcp{#1}%
  \edef\thekcatcode{\the\kcatcode\pxkq@cp}}
%%<*> \setkcatcode{<jachar>}{<value>}
\def\setkcatcode#1#2{\pxkq@getcp{#1}\kcatcode\pxkq@cp#2}
\newcount\pxkq@cp \newif\ifpxkq@ok
\def\pxkq@getcp#1{\pxkq@getcp@a#1\relax\relax}
\def\pxkq@nxt#1#2#3#4#5{\pxkq@cp#1\loop
  {\lccode`\?\pxkq@cp \advance\pxkq@cp-#3%
   \lowercase{\expandafter\xdef\csname pxkq@#4?\endcsname}{#5}}%
  \advance\pxkq@cp\@ne \ifnum\pxkq@cp<#2\repeat}
\pxkq@nxt{240}{248}{240}l{\pxkq@cp\the\pxkq@cp\noexpand\pxkq@da}
\pxkq@nxt{224}{240}{224}l{\pxkq@cp\the\pxkq@cp\noexpand\pxkq@ca}
\pxkq@nxt{194}{224}{192}l{\pxkq@cp\the\pxkq@cp\noexpand\pxkq@ba}
\pxkq@nxt{128}{192}{128}t{\advance\pxkq@cp\the\pxkq@cp\noexpand\pxkq@oktrue}
\def\pxkq@getcp@a#1{\pxkq@cp`#1\relax\pxkq@chkk#1%
  \ifpxkq@ok \let\pxkq@nxt\pxkq@end \else
    \expandafter\let\expandafter\pxkq@nxt\csname pxkq@l\string#1\endcsname
    \ifx\pxkq@nxt\relax \let\pxkq@nxt\pxkq@err \fi
  \fi \pxkq@nxt}
\def\pxkq@stp#1#2{\multiply\pxkq@cp64\pxkq@okfalse
  \csname pxkq@t\string#2\endcsname
  \ifpxkq@ok \expandafter#1\else \expandafter\pxkq@err \fi}
\def\pxkq@end#1\relax{}
\def\pxkq@ba{\pxkq@stp\pxkq@end}\def\pxkq@ca{\pxkq@stp\pxkq@cb}
\def\pxkq@cb{\pxkq@stp\pxkq@end}\def\pxkq@da{\pxkq@stp\pxkq@db}
\def\pxkq@db{\pxkq@stp\pxkq@dc}\def\pxkq@dc{\pxkq@stp\pxkq@end}
\begingroup\lccode`8=`k\lccode`9=`a \lowercase{\endgroup
 \def\pxkq@chkk#1{\pxkq@okfalse\expandafter\pxkq@chkk@a\meaning#189\@nil}
 \def\pxkq@chkk@a#189#2\@nil{\ifcat @#1@\pxkq@oktrue\fi}}
\def\pxkq@err#1\relax{\errhelp{I regard it as U+FFFF}%
  \errmessage{Corrupted UTF-8 bytes}\pxkq@cp65535\relax}
%\def\pxkq@end#1\relax{\showthe\pxkq@cp}

%--------------------------------------- all done
\pxkq@restore@codes
\endinput
%% EOF
