MCPcopy Create free account
hub / github.com/anjo76/angelscript / asStringEncodeUTF8

Function asStringEncodeUTF8

sdk/angelscript/source/as_string_util.cpp:238–275  ·  view source on GitHub ↗

The function will encode the unicode code point into the outEncodedBuffer, and then return the length of the encoded value. If the input value is not a valid unicode code point, then the function will return -1. This function is taken from the AngelCode ToolBox.

Source from the content-addressed store, hash-verified

236// This function is taken from the AngelCode ToolBox.
237//
238int asStringEncodeUTF8(unsigned int value, char *outEncodedBuffer)
239{
240 unsigned char *buf = (unsigned char*)outEncodedBuffer;
241
242 int length = -1;
243
244 if( value <= 0x7F )
245 {
246 buf[0] = static_cast<unsigned char>(value);
247 return 1;
248 }
249 else if( value >= 0x80 && value <= 0x7FF )
250 {
251 // Encode it with 2 characters
252 buf[0] = static_cast<unsigned char>(0xC0 + (value >> 6));
253 length = 2;
254 }
255 else if( (value >= 0x800 && value <= 0xD7FF) || (value >= 0xE000 && value <= 0xFFFF) )
256 {
257 // Note: Values 0xD800 to 0xDFFF are not valid unicode characters
258 buf[0] = static_cast<unsigned char>(0xE0 + (value >> 12));
259 length = 3;
260 }
261 else if( value >= 0x10000 && value <= 0x10FFFF )
262 {
263 buf[0] = static_cast<unsigned char>(0xF0 + (value >> 18));
264 length = 4;
265 }
266
267 int n = length-1;
268 for( ; n > 0; n-- )
269 {
270 buf[n] = static_cast<unsigned char>(0x80 + (value & 0x3F));
271 value >>= 6;
272 }
273
274 return length;
275}
276
277//
278// The function will decode an UTF8 character and return the unicode code point.

Callers 1

ProcessStringConstantMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected