c++ - std::string 操作:空白, "newline escapes '\'"和注释 #

标签 c++ algorithm string

有点想在这里寻求肯定。我有一些手写的代码,我并不羞于说我为此感到自豪,它读取一个文件,删除前导空格,处理换行转义 '\' 并删除以 # 开头的注释。它还会删除所有空行(也包括只有空白的行)。有什么想法/建议吗?我可能可以用 std::runtime_errors 替换一些 std::cout ......但这不是这里的优先事项:)

const int RecipeReader::readRecipe()
{
    ifstream is_recipe(s_buffer.c_str());
    if (!is_recipe)
        cout << "unable to open file" << endl;
    while (getline(is_recipe, s_buffer))
    {
        // whitespace+comment
        removeLeadingWhitespace(s_buffer);
        processComment(s_buffer);
        // newline escapes + append all subsequent lines with '\'
        processNewlineEscapes(s_buffer, is_recipe);
        // store the real text line
        if (!s_buffer.empty())
            v_s_recipe.push_back(s_buffer);
        s_buffer.clear();
    }
    is_recipe.close();
    return 0;
}

void RecipeReader::processNewlineEscapes(string &s_string, ifstream &is_stream)
{
    string s_temp;
    size_t sz_index = s_string.find_first_of("\\");
    while (sz_index <= s_string.length())
    {
        if (getline(is_stream,s_temp))
        {
            removeLeadingWhitespace(s_temp);
            processComment(s_temp);
            s_string = s_string.substr(0,sz_index-1) + " " + s_temp;
        }
        else
            cout << "Error: newline escape '\' found at EOF" << endl;
        sz_index = s_string.find_first_of("\\");
    }
}

void RecipeReader::processComment(string &s_string)
{
    size_t sz_index = s_string.find_first_of("#");
    s_string = s_string.substr(0,sz_index);
}

void RecipeReader::removeLeadingWhitespace(string &s_string)
{
    const size_t sz_length = s_string.size();
    size_t sz_index = s_string.find_first_not_of(" \t");
    if (sz_index <= sz_length)
    s_string = s_string.substr(sz_index);
    else if ((sz_index > sz_length) && (sz_length != 0)) // "empty" lines with only whitespace
        s_string.clear();
}

一些额外信息:传递给 ifstream 的第一个 s_buffer 包含文件名,std::string s_buffer 是类数据成员,std::vector v_s_recipe 也是。欢迎任何评论:)

更新:为了不忘恩负义,这是我的替代品,一体式功能,它可以满足我现在的需求( future 适用:括号,也许引号...):

void readRecipe(const std::string &filename)
{
    string buffer;
    string line;
    size_t index;
    ifstream file(filename.c_str());
    if (!file)
        throw runtime_error("Unable to open file.");

    while (getline(file, line))
    {
        // whitespace removal
        line.erase(0, line.find_first_not_of(" \t\r\n\v\f"));
        // comment removal TODO: store these for later output
        index = line.find_first_of("#");
        if (index != string::npos)
            line.erase(index, string::npos);
        // ignore empty buffer
        if (line.empty())
            continue;
        // process newline escapes
        index = line.find_first_of("\\");
        if (index != string::npos)
        {
            line.erase(index,string::npos); // ignore everything after '\'
            buffer += line;
            continue; // read next line
        }
        else // no newline escapes found
        {
            buffer += line;
            recipe.push_back(buffer);
            buffer.clear();
        }
    }
}

最佳答案

绝对放弃匈牙利符号。

关于c++ - std::string 操作:空白, "newline escapes '\'"和注释 #,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2888521/

相关文章:

c++ - SKIA::获取 Canvas 内文本的文本高度

c++ - 隐式的 new 和 delete 运算符杀死性能

c++ - PostgreSQL C API (libpq) 是否允许您使用结果而不是存储结果?

java - 似乎无法理解二叉搜索树中的递归

c - 如何 scanf ("%[^\n]", str);在 C 编程工作?

c++ - 在自定义 C++ 异常类中消失的字符串

algorithm - 证明一般树的树遍历算法的时间复杂度

C++ 二项式分布

python - 从文本文件的行中提取数据

c - 在c程序中接收后字符串以/UN结尾?