Esempio n. 1
0
bool parse_format_string(
  const exprt &format_arg,
  format_token_listt &token_list)
{
  token_list.clear();

  if(format_arg.id()==ID_string_constant)
  {
    const std::string &arg_string = id2string(format_arg.get(ID_value));

    std::string::const_iterator it=arg_string.begin();

    while(it!=arg_string.end())
    {
      if(*it=='%')
      {
        token_list.push_back(format_tokent());
        format_tokent &curtok=token_list.back();
        it++;

        parse_flags(it, curtok);
        parse_field_width(it, curtok);
        parse_precision(it, curtok);
        parse_length_modifier(it, curtok);
        parse_conversion_specifier(arg_string, it, curtok);
      }
      else
      {
        if(token_list.back().type!=format_tokent::TEXT)
          token_list.push_back(format_tokent(format_tokent::TEXT));

        std::string tmp;
        for(;it!=arg_string.end() && *it!='%';it++)
          tmp+=*it;

        token_list.back().value=tmp;
      }
    }

    return true;
  }

  return false; // non-const format string
}
Esempio n. 2
0
format_token_listt parse_format_string(const std::string &arg_string)
{
  format_token_listt token_list;

  std::string::const_iterator it=arg_string.begin();

  while(it!=arg_string.end())
  {
    if(*it=='%')
    {
      token_list.push_back(format_tokent());
      format_tokent &curtok=token_list.back();
      it++;

      parse_flags(it, curtok);
      parse_field_width(it, curtok);
      parse_precision(it, curtok);
      parse_length_modifier(it, curtok);
      parse_conversion_specifier(arg_string, it, curtok);
    }
    else
    {
      if(token_list.empty() ||
         token_list.back().type!=format_tokent::TEXT)
        token_list.push_back(format_tokent(format_tokent::TEXT));

      std::string tmp;
      for(;it!=arg_string.end() && *it!='%';it++)
        tmp+=*it;

      assert(!token_list.empty());
      token_list.back().value=tmp;
    }
  }

  return token_list;
}