• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision128 (tree)
Time2018-09-13 06:41:57
Authorquiret

Log Message

- converted UniChar.h to use eir::String instead to have ultimate control over Eir SDK memory usage

Change Summary

Incremental Difference

--- unittests/src/encodings.cpp (revision 127)
+++ unittests/src/encodings.cpp (revision 128)
@@ -8,6 +8,8 @@
88
99 #include <sdk/UniChar.h>
1010
11+#include "alloc_helper.hxx"
12+
1113 template <typename charType, typename char_prov>
1214 static size_t get_string_charlen( const charType *str, char_prov&& prov )
1315 {
@@ -70,12 +72,12 @@
7072 {
7173 const char *utf8_string = utf8_test_strings[n];
7274
73- std::wstring meowstr = CharacterUtil::ConvertStrings <char8_t, wchar_t> ( (const char8_t*)utf8_string );
75+ eir::String <wchar_t, EirHeapAllocator> meowstr = CharacterUtil::ConvertStrings <char8_t, wchar_t, EirHeapAllocator> ( (const char8_t*)utf8_string );
7476
7577 // Convert it back to utf8.
7678 auto resultStr = CharacterUtil::ConvertStrings <wchar_t, char8_t> ( meowstr );
7779
78- const char *newgen = (const char*)resultStr.c_str();
80+ const char *newgen = (const char*)resultStr.GetConstString();
7981
8082 assert( strlen( newgen ) == strlen( utf8_string ) );
8183 assert( UniversalCompareStrings( newgen, strlen(newgen), utf8_string, strlen(utf8_string), true ) == true );
@@ -92,10 +94,10 @@
9294 const char16_t *typical_wide_str = u"meow hello my friend";
9395 const char32_t *very_wide_str = U"meow hello my friend";
9496
95- std::string wide_conv = CharacterUtil::ConvertStrings <wchar_t, char> ( wide_str );
96- std::string utf8_conv = CharacterUtil::ConvertStrings <char8_t, char> ( utf8_str );
97- std::string typical_wide_conv = CharacterUtil::ConvertStrings <char16_t, char> ( typical_wide_str );
98- std::string very_wide_conv = CharacterUtil::ConvertStrings <char32_t, char> ( very_wide_str );
97+ auto wide_conv = CharacterUtil::ConvertStrings <wchar_t, char, EirHeapAllocator> ( wide_str );
98+ auto utf8_conv = CharacterUtil::ConvertStrings <char8_t, char, EirHeapAllocator> ( utf8_str );
99+ auto typical_wide_conv = CharacterUtil::ConvertStrings <char16_t, char, EirHeapAllocator> ( typical_wide_str );
100+ auto very_wide_conv = CharacterUtil::ConvertStrings <char32_t, char, EirHeapAllocator> ( very_wide_str );
99101
100102 assert( wide_conv == ascii_str );
101103 assert( utf8_conv == ascii_str );
@@ -107,12 +109,12 @@
107109 // Check validity of encoding implementations.
108110 printf( "testing ANSI to Unicode character conversions..." );
109111 {
110- std::string ascii_sample = "this is to be encoded, meow";
112+ eir::String <char, EirHeapAllocator> ascii_sample = "this is to be encoded, meow";
111113
112- std::basic_string <char8_t> utf8_target = CharacterUtil::ConvertStrings <char, char8_t> ( ascii_sample );
113- std::wstring wide_target = CharacterUtil::ConvertStrings <char, wchar_t> ( ascii_sample );
114- std::u16string typical_wide_target = CharacterUtil::ConvertStrings <char, char16_t> ( ascii_sample );
115- std::u32string very_wide_target = CharacterUtil::ConvertStrings <char, char32_t> ( ascii_sample );
114+ auto utf8_target = CharacterUtil::ConvertStrings <char, char8_t> ( ascii_sample );
115+ auto wide_target = CharacterUtil::ConvertStrings <char, wchar_t> ( ascii_sample );
116+ auto typical_wide_target = CharacterUtil::ConvertStrings <char, char16_t> ( ascii_sample );
117+ auto very_wide_target = CharacterUtil::ConvertStrings <char, char32_t> ( ascii_sample );
116118
117119 assert( UniversalCompareStrings( ascii_sample, utf8_target, true ) == true );
118120 assert( UniversalCompareStrings( ascii_sample, wide_target, true ) == true );
@@ -191,10 +193,10 @@
191193 {
192194 const char utf8_string_len2[] = u8"Спасибо";
193195 const char utf8_string_len3[] = u8"☀☁☂";
194- const char utf8_string_len4[] = u8"𠺝𠻺𡁜𠺢";
195-
196- static_assert( sizeof( utf8_string_len2 ) == 15, "len2 utf8 sequence broken" );
197- static_assert( sizeof( utf8_string_len3 ) == 10, "len3 utf8 sequence broken" );
196+ const char utf8_string_len4[] = u8"𠺝𠻺𡁜𠺢";
197+
198+ static_assert( sizeof( utf8_string_len2 ) == 15, "len2 utf8 sequence broken" );
199+ static_assert( sizeof( utf8_string_len3 ) == 10, "len3 utf8 sequence broken" );
198200 static_assert( sizeof( utf8_string_len4 ) == 17, "len4 utf8 sequence broken" );
199201
200202 assert( verify_exception( (const char8_t*)utf8_string_len2, 1 ) == true );
--- common/sdk/UniChar.strmodel.h (revision 127)
+++ common/sdk/UniChar.strmodel.h (revision 128)
@@ -15,8 +15,6 @@
1515
1616 #include "MacroUtils.h"
1717
18-#include <string>
19-
2018 //TODO: find ways to share as much code as possible between the different string model functions!
2119
2220 // IMPORTANT: this function is designed to terminate on '\0' inside any string aswell.
@@ -151,17 +149,17 @@
151149 return StringComparatorGeneric( srcStr, dstStr, caseSensitive, srcProc, dstProc );
152150 }
153151
154-template <typename srcCharType, typename dstCharType>
152+template <typename srcCharType, typename dstCharType, typename srcAllocatorType, typename dstAllocatorType>
155153 inline bool UniversalCompareStrings(
156- const std::basic_string <srcCharType>& src,
157- const std::basic_string <dstCharType>& dst,
154+ const eir::String <srcCharType, srcAllocatorType>& src,
155+ const eir::String <dstCharType, dstAllocatorType>& dst,
158156 bool caseSensitive
159157 )
160158 {
161- boundedCharItemProc <srcCharType> srcProc( src.size() );
162- boundedCharItemProc <dstCharType> dstProc( dst.size() );
159+ boundedCharItemProc <srcCharType> srcProc( src.GetLength() );
160+ boundedCharItemProc <dstCharType> dstProc( dst.GetLength() );
163161
164- return StringComparatorGeneric( src.c_str(), dst.c_str(), caseSensitive, srcProc, dstProc );
162+ return StringComparatorGeneric( src.GetConstString(), dst.GetConstString(), caseSensitive, srcProc, dstProc );
165163 }
166164
167165 template <typename srcCharType, typename dstCharType>
--- common/sdk/UniChar.h (revision 127)
+++ common/sdk/UniChar.h (revision 128)
@@ -15,10 +15,10 @@
1515
1616 #include <cstdint>
1717 #include "Endian.h"
18+#include "String.h"
1819
1920 #include <locale>
2021 #include <type_traits>
21-#include <string>
2222
2323 #include <limits>
2424
@@ -1026,8 +1026,8 @@
10261026 }
10271027 }
10281028
1029- template <typename inputCharType, typename outputCharType, typename char_prov_t>
1030- inline void _TranscodeStringWithCharProv( const inputCharType *inputChars, char_prov_t&& char_prov, std::basic_string <outputCharType>& output_str )
1029+ template <typename inputCharType, typename outputCharType, typename char_prov_t, typename allocatorType>
1030+ inline void _TranscodeStringWithCharProv( const inputCharType *inputChars, char_prov_t&& char_prov, eir::String <outputCharType, allocatorType>& output_str )
10311031 {
10321032 // TODO: optimize this function so that we reserve as many code-points into output_str as
10331033 // we would put into it by using a fast scan.
@@ -1063,12 +1063,12 @@
10631063 }
10641064 }
10651065
1066- template <typename inputCharType, typename outputCharType>
1067- inline std::basic_string <outputCharType> ConvertStrings( const inputCharType *inputChars )
1066+ template <typename inputCharType, typename outputCharType, typename allocatorType, typename... Args>
1067+ inline eir::String <outputCharType, allocatorType> ConvertStrings( const inputCharType *inputChars, Args... allocArgs )
10681068 {
10691069 try
10701070 {
1071- std::basic_string <outputCharType> output_str;
1071+ eir::String <outputCharType, allocatorType> output_str( nullptr, 0, std::forward <Args> ( allocArgs )... );
10721072
10731073 // Convert things into the output.
10741074 {
@@ -1086,19 +1086,19 @@
10861086 }
10871087 }
10881088
1089- template <typename inputCharType, typename outputCharType, bool optimizeEqual = true>
1090- inline std::basic_string <outputCharType> ConvertStringsLength( const inputCharType *inputChars, size_t inputLen )
1089+ template <typename inputCharType, typename outputCharType, typename allocatorType, bool optimizeEqual = true, typename... Args>
1090+ inline std::basic_string <outputCharType> ConvertStringsLength( const inputCharType *inputChars, size_t inputLen, Args... allocArgs )
10911091 {
10921092 if constexpr ( optimizeEqual && std::is_same <inputCharType, outputCharType>::value )
10931093 {
1094- return std::basic_string <outputCharType> ( inputChars, inputLen );
1094+ return eir::String <outputCharType, allocatorType> ( inputChars, inputLen, std::forward <Args> ( allocArgs )... );
10951095 }
10961096 else
10971097 {
1098+ eir::String <outputCharType, allocatorType> output_str( nullptr, 0, std::forward <Args> ( allocArgs )... );
1099+
10981100 try
10991101 {
1100- std::basic_string <outputCharType> output_str;
1101-
11021102 // Convert things into the output.
11031103 {
11041104 charenv_charprov_tocplen <inputCharType> char_prov( inputChars, inputLen );
@@ -1111,13 +1111,14 @@
11111111 catch( codepoint_exception& )
11121112 {
11131113 // On error we just return an error string.
1114- return GetDefaultConfErrorString <outputCharType> ();
1114+ output_str = GetDefaultConfErrorString <outputCharType> ();
1115+ return output_str;
11151116 }
11161117 }
11171118 }
11181119
1119- template <typename inputCharType, typename outputCharType, bool optimizeEqual = true>
1120- inline std::basic_string <outputCharType> ConvertStrings( const std::basic_string <inputCharType>& inputStr )
1120+ template <typename inputCharType, typename outputCharType, bool optimizeEqual = true, typename allocatorType, typename... Args>
1121+ inline eir::String <outputCharType, allocatorType> ConvertStrings( const eir::String <inputCharType, allocatorType>& inputStr, Args... allocArgs )
11211122 {
11221123 if constexpr ( optimizeEqual && std::is_same <inputCharType, outputCharType>::value )
11231124 {
@@ -1125,7 +1126,7 @@
11251126 }
11261127 else
11271128 {
1128- return ConvertStrings <inputCharType, outputCharType> ( inputStr.c_str() );
1129+ return ConvertStrings <inputCharType, outputCharType, allocatorType> ( inputStr.GetConstString(), std::forward <Args> ( allocArgs )... );
11291130 }
11301131 }
11311132 };
--- common/sdk/String.h (revision 127)
+++ common/sdk/String.h (revision 128)
@@ -49,11 +49,18 @@
4949
5050 static constexpr bool hasObjectAllocator = PERFORM_SUBSTRUCTCHECK( allocatorType, is_object );
5151
52+ AINLINE void reset_to_empty( void )
53+ {
54+ static charType empty_string[] = { (charType)0 };
55+
56+ this->data.char_data = empty_string;
57+ this->data.num_chars = 0;
58+ }
59+
5260 public:
5361 inline String( void )
5462 {
55- this->data.char_data = "";
56- this->data.num_chars = 0;
63+ this->reset_to_empty();
5764 }
5865
5966 private:
@@ -61,8 +68,7 @@
6168 {
6269 if ( initCharCount == 0 )
6370 {
64- this->data.char_data = "";
65- this->data.num_chars = 0;
71+ this->reset_to_empty();
6672 }
6773 else
6874 {
@@ -137,8 +143,7 @@
137143 this->data.char_data = right.data.char_data;
138144 this->data.num_chars = right.data.num_chars;
139145
140- right.data.char_data = "";
141- right.data.num_chars = 0;
146+ right.reset_to_empty();
142147 }
143148
144149 private:
@@ -205,7 +210,7 @@
205210 {
206211 this->release_data();
207212
208- this->data.char_data = "";
213+ this->reset_to_empty();
209214 }
210215 else
211216 {
@@ -231,10 +236,9 @@
231236 free_old_buffer( this, oldCharBuf, oldCharCount, isBufNew );
232237
233238 this->data.char_data = useBuf;
239+ this->data.num_chars = copyCharCount;
234240 }
235241 }
236-
237- this->data.num_chars = copyCharCount;
238242 }
239243
240244 inline String& operator = ( const String& right )
@@ -271,8 +275,7 @@
271275 this->data.char_data = right.data.char_data;
272276 this->data.num_chars = right.data.num_chars;
273277
274- right.data.char_data = "";
275- right.data.num_chars = 0;
278+ right.reset_to_empty();
276279
277280 return *this;
278281 }
Show on old repository browser