summaryrefslogtreecommitdiff
path: root/Source/Library/Common/WChar.class.h
blob: 6db4b1bb7a8583b898aa1785eb8cb7dd553d502d (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
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef DEF_UCHAR_CLASS_H
#define DEF_UCHAR_CLASS_H

#include <types.h>

#ifndef THIS_IS_NOT_MELON
#include <common.h>
#endif

#define EOF "\3"

enum {
	UE_UTF8,
	UE_UTF16_LE,
	UE_UTF16_BE,
	UE_UTF32_LE,
	UE_UTF32_BE,
};

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 affectUtf16le(const char* c);
	u32int affectUtf16be(const char* c);
	u32int affectUtf32le(const char* c);
	u32int affectUtf32be(const char* c);

	u32int affect(const char* c, u8int encoding = UE_UTF8) {
		if (encoding == UE_UTF8) return affectUtf8(c);
		if (encoding == UE_UTF16_LE) return affectUtf16le(c);
		if (encoding == UE_UTF16_BE) return affectUtf16be(c);
		if (encoding == UE_UTF32_LE) return affectUtf32le(c);
		if (encoding == UE_UTF32_BE) return affectUtf32be(c);
		affectAscii(c[0]);	//Default case :/
		return 1;
	}

	u8int toAscii();

	uchar_repr_t toUtf8();
	uchar_repr_t toUtf16le();
	uchar_repr_t toUtf16be();
	uchar_repr_t toUtf32le();
	uchar_repr_t toUtf32be();

	uchar_repr_t encode(u8int encoding = UE_UTF8) {
		if (encoding == UE_UTF8) return toUtf8();
		//if (encoding == UE_UTF16_LE) return toUtf16le();
		//if (encoding == UE_UTF16_BE) return toUtf16be();
		if (encoding == UE_UTF32_LE) return toUtf32le();
		if (encoding == UE_UTF32_BE) return toUtf32be();
		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 () const { return value; }
};

#endif