瀏覽代碼

Added LineReader.

Бранимир Караџић 6 年之前
父節點
當前提交
6124940cde
共有 2 個文件被更改,包括 64 次插入0 次删除
  1. 39 0
      include/bx/inline/string.inl
  2. 25 0
      include/bx/string.h

+ 39 - 0
include/bx/inline/string.inl

@@ -241,4 +241,43 @@ namespace bx
 		return StringView(_str, _start, _len);
 	}
 
+	inline LineReader::LineReader(const bx::StringView& _str)
+		: m_str(_str)
+	{
+		reset();
+	}
+
+	inline void LineReader::reset()
+	{
+		m_curr = m_str;
+		m_line = 0;
+	}
+
+	inline StringView LineReader::next()
+	{
+		if (m_curr.getPtr() != m_str.getTerm() )
+		{
+			++m_line;
+
+			StringView curr(m_curr);
+			m_curr = bx::strFindNl(m_curr);
+
+			StringView line(curr.getPtr(), m_curr.getPtr() );
+
+			return strRTrim(line, "\n\r");
+		}
+
+		return m_curr;
+	}
+
+	inline bool LineReader::isDone() const
+	{
+		return m_curr.getPtr() == m_str.getTerm();
+	}
+
+	inline uint32_t LineReader::getLine() const
+	{
+		return m_line;
+	}
+
 } // namespace bx

+ 25 - 0
include/bx/string.h

@@ -321,6 +321,31 @@ namespace bx
 	/// Converts string to 32-bit unsigned integer value.
 	bool fromString(uint32_t* _out, const StringView& _str);
 
+	///
+	class LineReader
+	{
+	public:
+		///
+		LineReader(const bx::StringView& _str);
+
+		///
+		void reset();
+
+		///
+		StringView next();
+
+		///
+		bool isDone() const;
+
+		///
+		uint32_t getLine() const;
+
+	private:
+		const bx::StringView m_str;
+		bx::StringView m_curr;
+		uint32_t m_line;
+	};
+
 } // namespace bx
 
 #include "inline/string.inl"