我想从 printf 格式字符串中提取 C++ 类型信息。例如,
Input: "%10u foo %% %+6.3f %ld %s"
Output:
unsigned int
double
long
char*
Run Code Online (Sandbox Code Playgroud)
我尝试使用printf.h 中的parse_printf_format()进行此操作 ,但返回的 argtypes 似乎不包含有关签名/未签名的信息。
有没有办法获取签名/未签名信息?
正如我在回答中所说, parse_printf_format 并不是根据您的需要而设计的。您可以通过以下算法自己解析它:
%
要么是修饰符,要么是类型(不能同时是两者),因此您首先%
在字符串中搜索 char您可以对真正的算法使用许多实现(例如boost),但由于您不需要验证输入字符串,因此手动操作非常简单。
伪代码:
const char flags[] = {'-', '+', '0', ' ', '#'};
const char widthPrec[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '*'}; // Last char is an extension
const char modifiers[] = { 'h', 'l', 'L', 'z', 'j', 't' };
const char types[] = { '%', 'd', 'i', 'u', 'f', 'F', 'e', 'E', 'g', 'G', 'x', 'X', 'a', 'A', 'o', 's', 'c', 'p', 'n' }; // Last one is not wanted too
const char validChars[] = { union of all arrays above };
enum Type { None = 0, Int, Unsigned, Float, etc... };
Type typesToType[] = { None, Int, Int, Unsigned, Float, Float, ... etc... }; // Should match the types array above
// Expect a valid format, not validation is done
bool findTypesInFormat(string & format, vector<Type> types)
{
size_t pos = 0;
types.clear();
while (pos < format.length())
{
pos = format.find_first_of('%', pos);
if (pos == format.npos) break;
pos++;
if (format[pos] == '%') continue;
size_t acceptUntilType = format.find_first_not_of(validChars, pos);
if (pos == format.npos) pos = format.length();
pos --;
if (!inArray(types, format[pos])) return false; // Invalid string if the type is not what we support
Type type = typesToType[indexInArray(types, format[pos])];
// We now know the type, we might need to refine it
if (inArray(modifiers, format[pos-1])
{
type = adjustTypeFromModifier(format[pos-1], type);
}
types.push_back(type);
pos++;
}
return true;
}
// inArray, indexInArray and adjustTypeFromModifier are simple functions left to be written.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2895 次 |
最近记录: |