aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/replxx/src/utf8string.hxx
blob: 3adf17a3492dfe24a064f69d8e946a0acb268cbe (plain)
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
#ifndef REPLXX_UTF8STRING_HXX_INCLUDED
#define REPLXX_UTF8STRING_HXX_INCLUDED

#include <memory>

#include "unicodestring.hxx"

namespace replxx {

class Utf8String {
private:
	typedef std::unique_ptr<char[]> buffer_t;
	buffer_t _data;
	int _bufSize;
public:
	Utf8String( void )
		: _data()
		, _bufSize( 0 ) {
	}
	explicit Utf8String( UnicodeString const& src )
		: _data()
		, _bufSize( 0 ) {
		assign( src, src.length() );
	}

	Utf8String( UnicodeString const& src_, int len_ )
		: _data()
		, _bufSize( 0 ) {
		assign( src_, len_ );
	}

	void assign( UnicodeString const& str_ ) {
		assign( str_, str_.length() );
	}

	void assign( UnicodeString const& str_, int len_ ) {
		int len( len_ * 4 );
		realloc( len );
		copyString32to8( _data.get(), len, str_.get(), len_ );
	}

	void assign( std::string const& str_ ) {
		realloc( str_.length() );
		strncpy( _data.get(), str_.c_str(), str_.length() );
	}

	char const* get() const {
		return _data.get();
	}

private:
	void realloc( int reqLen ) {
		if ( ( reqLen + 1 ) > _bufSize ) {
			_bufSize = 1;
			while ( ( reqLen + 1 ) > _bufSize ) {
				_bufSize *= 2;
			}
			_data.reset( new char[_bufSize] );
			memset( _data.get(), 0, _bufSize );
		}
		_data[reqLen] = 0;
		return;
	}
	Utf8String(const Utf8String&) = delete;
	Utf8String& operator=(const Utf8String&) = delete;
};

}

#endif