%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Low-level helper methods for string parsing and number conversion.
%
% This file comes as part of the pgfplots-package.
%
% Copyright 2007/2008 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{pgfplotshelpers}[2008/02/17 Version 0.9.4]

\newtoks\pgfplots@toka

\def\prettyprintnumberprecision{2}

\def\pgfmathlog@tonumber#1pt{#1}

% Invokes command #1 with #2 without the 'pt' suffix.
\def\invokepgf@math@one#1#2{%
	%\edef\pgfmath@resulttemp{\pgf@sys@tonumber{#2}}%
	%\expandafter#1\expandafter{\pgfmath@resulttemp}%
	\edef\pgfmath@resulttemp{{\pgf@sys@tonumber{#2}}}%
	\expandafter#1\pgfmath@resulttemp%
}

% Expands #2 using \edef and invokes #1 with the resulting string.
%
% Example:
%   \pgfmath@y=7.9pt
%   \pgfmathlog@invoke@expanded\pgfmathexp@{{\pgf@sys@tonumber{\pgfmath@y}}}%
% will invoke
%   \pgfmathexp@{7.9}
\def\pgfmathlog@invoke@expanded#1#2{%
	\edef\pgfmath@resulttemp{#2}%
	\expandafter#1\pgfmath@resulttemp
}

% Expands the NUMBER (without units) to a pretty printed string argument: rounded to
% several digits, discard any suffix zeros etc.
%
% It uses \prettyprintnumberprecision digits after the comma.
\def\prettyprintnumber#1{%
	\begingroup
%	\edef\argument{#1}%
%	\expandafter\prettyprintnumber@impl\argument.\END
	\edef\argument{#1\noexpand\prec\prettyprintnumberprecision}%
	\expandafter\fixedpointround\argument\to\result
	\result
	\endgroup
}

\def\prettyprintnumber@impl#1.#2\END{%
	% This implementation is really bad.
	% I hope I find time to get something better...
	\def\tmp{#2}%
	\ifx\tmp\empty
		% there was no '.' in the original arg
		#1
	\else
		% ok, there is a dot in the arg.
		% 1. ROUND
		% 2. DISCARD ZEROS
		\toksdef\tokens=2
		\let\i=\c@pgf@counta
		\let\lastdigit=\c@pgf@countb
		\let\numberAsDIM=\pgfmath@x
		\let\zeroFilledNum=\pgfmath@y
		\i=0
		\tokens={}%
		\def\D{#2}%
		\def\DOT{.}%
		\ifx\D\DOT
			% the number '#1.'
			\let\D=\empty
		\else
			\expandafter\prettyprintnumber@impl@getfirst
				\prettyprintnumberprecision
				\of#2%
				\to\tokens%
				\nextto\D
				\count\i
		\fi
		%\message{Pretty print: first <= \prettyprintnumberprecision\ digits after period: \the\tokens, (strlen=\the\i), next rounding-relevant-digit: '\D'}%
		\ifx\D\empty
			\numberAsDIM=#1\expandafter.\the\tokens pt
			% Nothing to do - we have our desired precision.
		\else
			\lastdigit\expandafter=\D\relax
%		\message{D=\D!}%
			\ifnum\lastdigit>4
				\numberAsDIM=#1\expandafter.\expandafter\the\tokens\D pt
				% round up:
				% compute numberAsDIM += 0.0000[10-digit]
				\advance\lastdigit by-10
				\multiply\lastdigit by-1
				\tokens={0.}%
				\loop
				\ifnum\i>0
					\tokens=\expandafter{\the\tokens0}%
					\advance\i by-1
				\repeat
				\edef\D{\the\tokens\the\lastdigit pt}%
				\expandafter\zeroFilledNum\D
				\ifdim\numberAsDIM<0pt
					\advance\numberAsDIM by-\zeroFilledNum
				\else
					\advance\numberAsDIM by\zeroFilledNum
				\fi
			\else
				\numberAsDIM=#1\expandafter.\the\tokens pt
			\fi
		\fi
		%\message{Pretty print: Result after rounding = \the\numberAsDIM / zeroFilledNum = \the\zeroFilledNum [only defined on up-rounding}%
		\edef\D{\pgf@sys@tonumber{\numberAsDIM}}%
		\expandafter\prettyprintnumber@onlyrelevant@digits\D\to\D
		% FIXME : I could return '\D' as macro!? May thats a better
		% way...
		\D
	\fi
}

% This macro takes the first \prettyprintnumberprecision digits after
% the period and squeezes away any trailing zeros.
%
% If no digits after the period remain, the period will be discarded
% as well.
\def\prettyprintnumber@onlyrelevant@digits#1.#2\to#3{%
	\begingroup
	\toksdef\tokens=2
	\let\i=\c@pgf@counta
	\let\maxDigits=\c@pgf@countc
	\let\numsuffixZeros=\c@pgf@countb
	\tokens={#1.}%
	\i=0
	\expandafter\maxDigits\prettyprintnumberprecision
	\numsuffixZeros=0
	\prettyprintnumber@onlyrelevant@digits@leftshift#2\to#3
	\ifnum\numsuffixZeros>0
		\ifnum\numsuffixZeros=\i
			\tokens={#1}%
		\else
			\maxDigits=\i
			\advance\maxDigits by-\numsuffixZeros
			\tokens={#1.}%
			\i=0
			\prettyprintnumber@onlyrelevant@digits@leftshift#2\to#3
		\fi
	\fi
	\xdef\pgfplotshelpersTMP{\the\tokens}%
	\endgroup
	\let#3=\pgfplotshelpersTMP
}


% shifts the input argument until either no more input is left or
% \maxDigits entries have been acquired.
%
% It also updates \i (will contain the number of acquired elements)
% and \numsuffixZeros (the number of zeros at the tail of #3 on
% output).
\def\prettyprintnumber@onlyrelevant@digits@leftshift#1#2\to#3{%
	\ifnum\i<\maxDigits
		\tokens=\expandafter{\the\tokens#1}%
		\advance\i by1
		\ifnum#1=0
			\advance\numsuffixZeros by1
		\else
			\numsuffixZeros=0
		\fi
		\def\tmp{#2}%
		\ifx\tmp\empty
		\else
			\prettyprintnumber@onlyrelevant@digits@leftshift#2\to#3
		\fi
	\fi
}

\def\prettyprintnumber@impl@getfirst#1\of#2#3.\to#4\nextto#5\count#6{%
	\edef#5{#3}%
	#4=\expandafter{\the#4#2}%
	\advance#6 by1
	\ifx#5\empty
	\else
		\ifnum#6<#1
			\prettyprintnumber@impl@getfirst #1\of#3.\to#4\nextto#5\count{#6}%
		\else
			\prettyprintnumber@impl@firstof#3\to#5
		\fi
	\fi
}

\def\prettyprintnumber@impl@firstof#1#2\to#3{\def#3{#1}}%

