// Creates a Format object parsing format string into format tokens (alphanumeric) and separators (non-alphanumeric).
public NumberFormatter(string formatString, int lang, string letterValue, string groupingSeparator, int groupingSize)
{
Debug.Assert(groupingSeparator.Length <= 1);
_formatString = formatString;
_lang = lang;
_letterValue = letterValue;
_groupingSeparator = groupingSeparator;
_groupingSize = groupingSeparator.Length > 0 ? groupingSize : 0;
if (formatString == "1" || formatString.Length == 0)
{
// Special case of the default format
return;
}
_tokens = new List <TokenInfo>();
int idxStart = 0;
bool isAlphaNumeric = CharUtil.IsAlphaNumeric(formatString[idxStart]);
if (isAlphaNumeric)
{
// If the first one is alpha num add empty separator as a prefix
_tokens.Add(null);
}
for (int idx = 0; idx <= formatString.Length; idx++)
{
// Loop until a switch from formatString token to separator is detected (or vice-versa)
if (idx == formatString.Length || isAlphaNumeric != CharUtil.IsAlphaNumeric(formatString[idx]))
{
if (isAlphaNumeric)
{
// Just finished a format token
_tokens.Add(TokenInfo.CreateFormat(formatString, idxStart, idx - idxStart));
}
else
{
// Just finished a separator token
_tokens.Add(TokenInfo.CreateSeparator(formatString, idxStart, idx - idxStart));
}
// Begin parsing the next format token or separator
idxStart = idx;
// Flip flag from format token to separator or vice-versa
isAlphaNumeric = !isAlphaNumeric;
}
}
}