stringparser.hpp

来自「Octane v1.01.20 The Open Compression To」· HPP 代码 · 共 64 行

HPP
64
字号
//
// Simple string parser.
//
// Copyright (c) 2003 by Octane Project Group
// All Rights Reserved
//
// http://octane.sourceforge.net/
//

/// @file   stringparser.hpp
/// @brief  Simple string parser.
/// @author Jibz
/// @date   2003.08.01

#ifndef OCTANE_STRINGPARSER_HPP_INCLUDED
#define OCTANE_STRINGPARSER_HPP_INCLUDED

#include <string>

/// Simple string parser.
///
/// Breaks up a string into space-delimited tokens.
///
/// @if developer
/// The class operates on a copy of the string, to avoid any
/// synchronization issues. This should not be a problem for
/// performance if the string implementation is reference
/// counted.
/// @endif
///
/// @ingroup Utility
///
class stringparser {

public:
    /// Constructor.
    /// @param _s - the string to parse.
    stringparser(const std::string &_s)	: s(_s), idx(0) { ; }

    /// Get next token from string.
    /// @return the next token.
    /// @todo should be able to handle quoted tokens.
    std::string get_token()
    {
	if (idx == std::string::npos) return "";

	std::string::size_type p = s.find_first_not_of(' ', idx);

	if (p == std::string::npos) return "";

	idx = s.find_first_of(' ', p);

	if (idx == std::string::npos) return s.substr(p);

	return s.substr(p, idx - p);
    }

private:
    const std::string s;
    std::string::size_type idx;
};

#endif // OCTANE_STRINGPARSER_HPP_INCLUDED

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?