summaryrefslogtreecommitdiff
path: root/Source/Kernel/Library/WChar.class.h
blob: e4da603b21b26bf0c42c461aee3da90bf1cf0c28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef DEF_UCHAR_CLASS_H
#define DEF_UCHAR_CLASS_H

#include <Core/types.wtf.h>

#ifndef THIS_IS_NOT_MELON
#include <Core/common.wtf.h>
#endif

enum {
	UE_UTF8,
	UE_UTF16,
	UE_UTF32,
};

union uchar_repr_t {
	char c[4];
	u32int i;
};

struct WChar {
	u32int value;
	static WChar CP437[];		//Codepage 437, used for conversion from/to ascii

	WChar();		//Creates a null character
	WChar(char c);	//From ascii character
	WChar(const char* c, u8int encoding = UE_UTF8);	//From utf8 string

	static u32int ucharLen(const char* c, u8int encoding = UE_UTF8);	//Returns count of bytes in one unicode character
	static u32int utfLen(const char* c, u8int encoding = UE_UTF8);	//Returns count of utf8 characters in string

	void affectAscii(char c);
	u32int affectUtf8(const char* c);
	u32int affectUtf16(const char* c);
	u32int affectUtf32(const char* c);

	u32int affect(const char* c, u8int encoding = UE_UTF8) {
		if (encoding == UE_UTF8) return affectUtf8(c);
		if (encoding == UE_UTF16) return affectUtf16(c);
		if (encoding == UE_UTF32) return affectUtf32(c);
		affectAscii(c[0]);	//Default case :/
		return 1;
	}

	u8int toAscii();

	uchar_repr_t toUtf8();
	uchar_repr_t toUtf16();
	uchar_repr_t toUtf32();

	uchar_repr_t encode(u8int encoding = UE_UTF8) {
		if (encoding == UE_UTF8) return toUtf8();
		//if (encoding == UE_UTF16) return toUtf16();
		if (encoding == UE_UTF32) return toUtf32();
		uchar_repr_t x;
		x.c[0] = toAscii();
		return x;
	}

	inline WChar operator+ (u32int other) {
		WChar r;
		r.value = value + other;
		return r;
	}
	inline WChar operator- (u32int other) {
		WChar r;
		r.value = value - other;
		return r;
	}
	inline WChar& operator+= (u32int other) {
		value += other;
		return *this;
	}
	inline WChar& operator-= (u32int other) {
		value -= other;
		return *this;
	}
	inline bool operator== (u32int other) {
		return value == other;
	}
	inline u32int operator= (u32int v) {
		value = v;
		return v;
	}

	inline operator u32int () { return value; }
};

#endif