从 printf 格式字符串中提取类型信息

dsh*_*hin 2 c++ linux printf

我想从 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 似乎不包含有关签名/未签名的信息。

有没有办法获取签名/未签名信息?

xry*_*669 5

正如我在回答中所说, parse_printf_format 并不是根据您的需要而设计的。您可以通过以下算法自己解析它:

  1. 因为后面的 char%要么是修饰符,要么是类型(不能同时是两者),因此您首先%在字符串中搜索 char
  2. 如果下一个字符位于类型数组('d'、's'、'f'、'g'、'u'等)中,那么您将获得该类型的类(指针、int、无符号) ,双倍,等等...)。这可能足以满足您的需要。
  3. 如果没有,则继续查找下一个字符,直到找到修饰符/类型数组中不允许的一个字符。
  4. 如果类型的类不足以满足您的需要,则必须返回修饰符来调整最终类型。

您可以对真正的算法使用许多实现(例如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)