1
0
Fork 0

DBG: added stringformatinline

This commit is contained in:
mrexodia 2016-05-22 14:43:50 +02:00
parent 12e320e612
commit 637aa0f1b9
2 changed files with 93 additions and 1 deletions

View File

@ -108,7 +108,7 @@ String stringformat(String format, const FormatValueVector & values)
i++;
continue;
}
else if(format[i] == '}' && (i + 1 < len && format[i + 1] == '}'))
if(format[i] == '}' && (i + 1 < len && format[i + 1] == '}'))
{
output += "}";
i++;
@ -137,4 +137,95 @@ String stringformat(String format, const FormatValueVector & values)
if(inFormatter && formatString.size())
output += handleFormatString(formatString, values);
return output;
}
static const char* getArgValueTypeInline(const String & formatString, ValueType::ValueType & type)
{
auto hasExplicitType = false;
if(formatString.size() > 2 && formatString[1] == ':')
{
switch(formatString[0])
{
case 'd':
type = ValueType::SignedDecimal;
hasExplicitType = true;
break;
case 'u':
type = ValueType::UnsignedDecimal;
hasExplicitType = true;
break;
case 'p':
type = ValueType::Pointer;
hasExplicitType = true;
break;
case 's':
type = ValueType::String;
hasExplicitType = true;
break;
default:
break;
}
}
auto expression = formatString.c_str();
if(hasExplicitType)
expression += 2;
else
type = ValueType::Hex;
return expression;
}
static String handleFormatStringInline(const String & formatString)
{
auto type = ValueType::Unknown;
auto value = getArgValueTypeInline(formatString, type);
if(value && *value)
return printValue(value, type);
return "[Formatting Error]";
}
String stringformatinline(String format)
{
StringUtils::ReplaceAll(format, "\\n", "\n");
int len = (int)format.length();
String output;
String formatString;
bool inFormatter = false;
for(int i = 0; i < len; i++)
{
//handle escaped format sequences "{{" and "}}"
if(format[i] == '{' && (i + 1 < len && format[i + 1] == '{'))
{
output += "{";
i++;
continue;
}
if(format[i] == '}' && (i + 1 < len && format[i + 1] == '}'))
{
output += "}";
i++;
continue;
}
//handle actual formatting
if(format[i] == '{' && !inFormatter) //opening bracket
{
inFormatter = true;
formatString.clear();
}
else if(format[i] == '}' && inFormatter) //closing bracket
{
inFormatter = false;
if(formatString.length())
{
output += handleFormatStringInline(formatString);
formatString.clear();
}
}
else if(inFormatter) //inside brackets
formatString += format[i];
else //outside brackets
output += format[i];
}
if(inFormatter && formatString.size())
output += handleFormatStringInline(formatString);
return output;
}

View File

@ -7,5 +7,6 @@ typedef const char* FormatValueType;
typedef std::vector<FormatValueType> FormatValueVector;
String stringformat(String format, const FormatValueVector & values);
String stringformatinline(String format);
#endif //_STRINGFORMAT_H