%!TEX pdflatex

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{xcolor}
\setlength\parindent{0pt}
\usepackage{codehigh}

\ExplSyntaxOn

\NewDocumentCommand\TestAndSplit{ O{norm} m m m m }{%
	% #2 = substr (le séparateur, par exemple "/")
	% #3 = str to test (la chaîne à tester, par exemple "10pt/5pt")
	% #4 = left macro (ou #2 si non trouvé)
	% #5 = right macro (ou #2 si non trouvé)
	\str_case:nnF { #1 }
	{
		{ dim }{ \testdimandsplit{#2}{#3}{#4}{#5} }
		{ col }{ \testcolandsplit{#2}{#3}{#4}{#5} }
	}
	{ \testandsplit{#2}{#3}{#4}{#5} }
}

\NewDocumentCommand\testdimandsplit{ m m m m }
{
	\tl_set:Nx \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_set:Nx \l_tmpb_tl { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_set:Nx \l_tmpc_tl { \seq_item:Nn \l_tmpa_seq {2} }
		\setlength{#3}{\l_tmpb_tl}
		\setlength{#4}{\l_tmpc_tl}
	}
	{
		\setlength{#3}{#2}
		\setlength{#4}{#2}
	}
}

\NewDocumentCommand{\testandsplit}{ m m m m }
{
	%#1 = substr
	%#2 = str to test
	%#3 = left macro (or #2 if not found)
	%#4 = right macro (or #2 if not found)
	\tl_set:Nx \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_set:Nx #3 { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_set:Nx #4 { \seq_item:Nn \l_tmpa_seq {2} }
	}
	{
		\tl_set:Nx #3 {#2}
		\tl_set:Nx #4 {#2}
	}
}

\NewDocumentCommand{\testcolandsplit}{ m m m m }
{
	%#1 = substr
	%#2 = str to test
	%#3 = left macro (or #2 if not found)
	%#4 = right macro (or #2 if not found)
	\tl_set:Nx \l_tmpa_tl { #2 }
	\tl_if_in:NnTF \l_tmpa_tl { #1 }
	{
		\seq_set_split:Nne \l_tmpa_seq { #1 } { #2 }
		\tl_set:Nx \l_tmpb_tl { \seq_item:Nn \l_tmpa_seq {1} }
		\tl_set:Nx \l_tmpc_tl { \seq_item:Nn \l_tmpa_seq {2} }
		\colorlet{#3}{\l_tmpb_tl}
		\colorlet{#4}{\l_tmpc_tl}
	}
	{
		\colorlet{#3}{#2}
		\colorlet{#4}{#2}
	}
}

\ExplSyntaxOff

\begin{document}

\section{strings}

\begin{demohigh}[language=latex/latex2]
\def\teststringA{A}
\def\teststringB{A/B}

\TestAndSplit{/}{L/R}{\stringleft}{\stringright}
\stringleft+\stringright\\
\TestAndSplit{/}{\teststringA}{\stringleft}{\stringright}
\stringleft+\stringright\\
\TestAndSplit{/}{\teststringB}{\stringleft}{\stringright}
\stringleft+\stringright
\end{demohigh}

\section{lengths}

\begin{demohigh}[language=latex/latex2]
\def\testlengthA{145pt}
\def\testlengthB{145pt/1cm}
\newlength\lengthleft
\newlength\lengthright

\TestAndSplit[dim]{/}{10cm/11cm}{\lengthleft}{\lengthright}
\the\lengthleft+\the\lengthright\\
\TestAndSplit[dim]{/}{\testlengthA}{\lengthleft}{\lengthright}
\the\lengthleft+\the\lengthright\\
\TestAndSplit[dim]{/}{\testlengthB}{\lengthleft}{\lengthright}
\the\lengthleft+\the\lengthright
\end{demohigh}

\section{colors}

\begin{demohigh}[language=latex/latex2]
\def\testcolorA{green}
\def\testcolorB{red/blue}

\TestAndSplit[col]{/}{pink/olive}{colorleft}{colorright}
\textcolor{colorleft}{left}+\textcolor{colorright}{right}\\
\TestAndSplit[col]{/}{\testcolorA}{colorleft}{colorright}
\textcolor{colorleft}{left}+\textcolor{colorright}{right}\\
\TestAndSplit[col]{/}{\testcolorB}{colorleft}{colorright}
\textcolor{colorleft}{left}+\textcolor{colorright}{right}
\end{demohigh}

\end{document}
