aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/patched/replxx/src/utf8string.hxx
blob: c7b1ea5b445059378895db4d4ccf25c351dae194 (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
#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;
	int _len;
public:
	Utf8String( void )
		: _data()
		, _bufSize( 0 )
		, _len( 0 ) {
	}
	explicit Utf8String( UnicodeString const& src )
		: _data()
		, _bufSize( 0 )
		, _len( 0 ) {
		assign( src, src.length() );
	}

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

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

	void assign( UnicodeString const& str_, int len_ ) {
		assign( str_.get(), len_ );
	}

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

	void assign( std::string const& str_ ) {
		realloc( static_cast<int>( str_.length() ) );
		strncpy( _data.get(), str_.c_str(), str_.length() );
		_len = static_cast<int>( str_.length() );
	}

	void assign( Utf8String const& other_ ) {
		realloc( other_._len );
		strncpy( _data.get(), other_._data.get(), other_._len );
		_len = other_._len;
	}

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

	int size( void ) const {
		return ( _len );
	}

	bool operator != ( Utf8String const& other_ ) {
		return (
			( other_._len != _len )
			|| (
				(  _len != 0 )
				&& ( memcmp( other_._data.get(), _data.get(), _len ) != 0 )
			)
		);
	}

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