-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(buffer.hpp): create Buffer and StringBuffer
- Loading branch information
1 parent
496128c
commit 17fd64c
Showing
10 changed files
with
291 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
#include <variant> | ||
#include <string> | ||
#include <string_view> | ||
#include <optional> | ||
|
||
namespace myxml | ||
{ | ||
/** | ||
* ADT. Used by Parser. Parser will consume the source. | ||
* Implement by StringBuffer and XMLFile | ||
*/ | ||
class Buffer | ||
{ | ||
public: | ||
virtual ~Buffer() = default; | ||
virtual std::optional<char> Peek() const = 0; | ||
virtual std::optional<std::string_view> PeekN(int) const = 0; | ||
virtual std::optional<char> AfterN(int) const = 0; | ||
virtual std::optional<std::string_view> AfterNM(int, int) const = 0; | ||
virtual std::optional<char> Take() = 0; | ||
virtual std::optional<std::string_view> TakeN(int) = 0; | ||
}; | ||
|
||
class StringBuffer : public Buffer | ||
{ | ||
private: | ||
using stringVariant = std::variant<std::string, std::string_view>; | ||
|
||
stringVariant inner; | ||
std::size_t offset; | ||
std::string_view getView() const; | ||
|
||
public: | ||
StringBuffer(std::string_view); | ||
StringBuffer(std::string &&); | ||
|
||
virtual std::optional<char> Peek() const override; | ||
virtual std::optional<std::string_view> PeekN(int) const override; | ||
virtual std::optional<char> AfterN(int) const override; | ||
virtual std::optional<std::string_view> AfterNM(int, int) const override; | ||
virtual std::optional<char> Take() override; | ||
virtual std::optional<std::string_view> TakeN(int) override; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,4 +57,9 @@ namespace myxml | |
public: | ||
UnexpectedEndOfInput(); | ||
}; | ||
|
||
class IOError : public std::exception | ||
{ | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
#include <memory> | ||
#include <string_view> | ||
|
||
namespace myxml | ||
{ | ||
class XMLFile | ||
{ | ||
private: | ||
XMLFile(); | ||
|
||
int fd; | ||
void *mappedRegion; | ||
|
||
public: | ||
static XMLFile Open(std::string_view fpath); | ||
~XMLFile(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "myxml/buffer.hpp" | ||
|
||
namespace myxml | ||
{ | ||
StringBuffer::StringBuffer(std::string_view inner) | ||
: inner(inner), offset(0) | ||
{ | ||
} | ||
|
||
StringBuffer::StringBuffer(std::string &&inner) | ||
: inner(inner), offset(0) | ||
{ | ||
} | ||
|
||
std::optional<char> StringBuffer::Peek() const | ||
{ | ||
auto view = this->getView(); | ||
if (offset >= view.length()) | ||
{ | ||
return std::nullopt; | ||
} | ||
return view[this->offset]; | ||
} | ||
|
||
std::optional<std::string_view> StringBuffer::PeekN(int n) const | ||
{ | ||
auto view = this->getView(); | ||
if (offset + n > view.length()) | ||
{ | ||
return std::nullopt; | ||
} | ||
return view.substr(this->offset, n); | ||
} | ||
|
||
std::optional<char> StringBuffer::AfterN(int n) const | ||
{ | ||
auto view = this->getView(); | ||
if (offset + n > view.length()) | ||
{ | ||
return std::nullopt; | ||
} | ||
return view[this->offset + n]; | ||
} | ||
|
||
std::optional<std::string_view> StringBuffer::AfterNM(int n, int m) const | ||
{ | ||
auto view = this->getView(); | ||
if (offset + n + m > view.length()) | ||
{ | ||
return std::nullopt; | ||
} | ||
return view.substr(offset + n, m); | ||
} | ||
|
||
std::optional<char> StringBuffer::Take() | ||
{ | ||
auto view = this->getView(); | ||
if (offset >= view.length()) | ||
{ | ||
return std::nullopt; | ||
} | ||
return view[this->offset++]; | ||
} | ||
|
||
std::optional<std::string_view> StringBuffer::TakeN(int n) | ||
{ | ||
auto view = this->getView(); | ||
if (offset + n >= view.length()) | ||
{ | ||
return std::nullopt; | ||
} | ||
std::string_view it = view.substr(offset, n); | ||
offset += n; | ||
return it; | ||
} | ||
|
||
std::string_view StringBuffer::getView() const | ||
{ | ||
if (std::holds_alternative<std::string>(this->inner)) | ||
{ | ||
return std::string_view(std::get<std::string>(this->inner)); | ||
} | ||
else | ||
{ | ||
return std::get<std::string_view>(this->inner); | ||
} | ||
} | ||
} |
Oops, something went wrong.