summaryrefslogblamecommitdiff
path: root/Source/Library/Common/WChar.class.h
blob: 5d6d26b53470d16504085007b230fb02f2fd4d4a (plain) (tree)
1
2
3
4
5
6
7
8
9


                         
                  

                         
                   
      
 










                    
              
                     
                                                                                         
 

                                                          
                                                                          
 
                                                                                                                                 
                                                                                                                    

                                 
                                         









                                                                
 

                        



                               








                                                             

                                               


                                        

                                               


                                        
                                                 


                               
                                                 










                                               
                                                         


      
#ifndef DEF_UCHAR_CLASS_H
#define DEF_UCHAR_CLASS_H

#include <types.h>

#ifndef THIS_IS_NOT_MELON
#include <common.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 () const { return value; }
};

#endif