2015-09-02 22:00:02 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "../common.h"
|
|
|
|
|
#include "../localisation/localisation.h"
|
|
|
|
|
#include "../util/util.h"
|
|
|
|
|
|
2015-09-04 23:27:47 +01:00
|
|
|
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;
|
2015-09-04 23:27:47 +01:00
|
|
|
virtual bool CanRead() const abstract;
|
2015-09-02 22:00:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class UTF8StringReader final : public IStringReader {
|
|
|
|
|
public:
|
2015-09-04 23:27:47 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 23:27:47 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 23:27:47 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 23:27:47 +01:00
|
|
|
void Skip() override {
|
2015-09-02 22:00:02 +01:00
|
|
|
int codepoint;
|
|
|
|
|
TryRead(&codepoint);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-04 23:27:47 +01:00
|
|
|
bool CanRead() const override {
|
|
|
|
|
return _current != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-02 22:00:02 +01:00
|
|
|
private:
|
|
|
|
|
const utf8 *_text;
|
|
|
|
|
const utf8 *_current;
|
|
|
|
|
};
|