Files
OpenRCT2-Web/src/core/StringReader.hpp
T

58 lines
1.1 KiB
C++
Raw Normal View History

2015-09-02 22:00:02 +01:00
#pragma once
#include "../common.h"
#include "../localisation/localisation.h"
#include "../util/util.h"
interface IStringReader {
2015-09-02 22:00:02 +01:00
virtual bool TryPeek(int *outCodepoint) abstract;
virtual bool TryRead(int *outCodepoint) abstract;
virtual void Skip() abstract;
virtual bool CanRead() const abstract;
2015-09-02 22:00:02 +01:00
};
class UTF8StringReader final : public IStringReader {
public:
UTF8StringReader(const utf8 *text) {
2015-09-02 22:00:02 +01:00
// Skip UTF-8 byte order mark
if (strlen(text) >= 3 && utf8_is_bom(text)) {
text += 3;
}
_text = text;
_current = text;
}
bool TryPeek(int *outCodepoint) override {
2015-09-02 22:00:02 +01:00
if (_current == NULL) return false;
int codepoint = utf8_get_next(_current, NULL);
*outCodepoint = codepoint;
return true;
}
bool TryRead(int *outCodepoint) override {
2015-09-02 22:00:02 +01:00
if (_current == NULL) return false;
int codepoint = utf8_get_next(_current, &_current);
*outCodepoint = codepoint;
if (codepoint == 0) {
_current = NULL;
return false;
}
return true;
}
void Skip() override {
2015-09-02 22:00:02 +01:00
int codepoint;
TryRead(&codepoint);
}
bool CanRead() const override {
return _current != NULL;
}
2015-09-02 22:00:02 +01:00
private:
const utf8 *_text;
const utf8 *_current;
};