Skip to content

Commit

Permalink
feat(buffer.hpp): create Buffer and StringBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamska1008 committed Aug 31, 2024
1 parent 496128c commit 17fd64c
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 83 deletions.
45 changes: 45 additions & 0 deletions include/myxml/buffer.hpp
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;
};
}
2 changes: 2 additions & 0 deletions include/myxml/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ namespace myxml
const std::shared_ptr<Element> &GetRoot() const;
std::shared_ptr<Element> GetRoot();

/** Load */
static std::optional<Document> Parse(std::string);
static std::optional<Document> ParseFile(std::string fileName);

/* Exportable */
virtual std::string ExportRaw() const;
Expand Down
5 changes: 5 additions & 0 deletions include/myxml/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ namespace myxml
public:
UnexpectedEndOfInput();
};

class IOError : public std::exception
{

};
}
15 changes: 9 additions & 6 deletions include/myxml/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "myxml/element.hpp"
#include "myxml/document.hpp"
#include "myxml/cdata.hpp"
#include "myxml/buffer.hpp"

namespace myxml
{
Expand All @@ -22,14 +23,15 @@ namespace myxml
class Parser
{
private:
std::string buffer;
std::size_t offset;
std::shared_ptr<Buffer> buffer;

std::optional<char> peek();
std::optional<std::string_view> peekN(int);
std::optional<char> afterN(int);
std::optional<std::string_view> afterNM(int, int);
std::optional<char> take();
std::optional<std::string_view> takeN(int);
void skipWhiteSpaces();
std::optional<char> peekChar();
std::optional<std::string> peekNextNChars(int);
std::optional<char> nextChar();
std::optional<std::string> nextNChars(int);

/**
* For all parsing method,
Expand Down Expand Up @@ -93,6 +95,7 @@ namespace myxml
Document ParseDocument();
Parser() = delete;
explicit Parser(std::string_view);
explicit Parser(std::string &&);
};

namespace util
Expand Down
19 changes: 19 additions & 0 deletions include/myxml/xmlfile.hpp
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();
};
}
88 changes: 88 additions & 0 deletions src/buffer.cpp
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);
}
}
}
Loading

0 comments on commit 17fd64c

Please sign in to comment.