Skip to content

Commit 2a21250

Browse files
committed
[[ Foundation ]] Add unicode string constructor
This patch adds a new string constructor `MCStringCreateUnicodeWithChars` which creates a string-ref from utf-16 codeunits, but forces it to be a unicode string (i.e. doesn't check to see if it is native).
1 parent c0265cc commit 2a21250

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

libfoundation/include/foundation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,10 @@ MC_DLLEXPORT bool MCStringCreateWithBytesAndRelease(byte_t *bytes, uindex_t byte
21802180
MC_DLLEXPORT bool MCStringCreateWithChars(const unichar_t *chars, uindex_t char_count, MCStringRef& r_string);
21812181
MC_DLLEXPORT bool MCStringCreateWithCharsAndRelease(unichar_t *chars, uindex_t char_count, MCStringRef& r_string);
21822182

2183+
// Create an immutable string from the given unicode char sequence and force it
2184+
// to be unicode.
2185+
MC_DLLEXPORT bool MCStringCreateUnicodeWithChars(const unichar_t *chars, uindex_t char_count, MCStringRef& r_string);
2186+
21832187
// Create an immutable string from the given NUL terminated unicode char sequence.
21842188
MC_DLLEXPORT bool MCStringCreateWithWString(const unichar_t *wstring, MCStringRef& r_string);
21852189
MC_DLLEXPORT bool MCStringCreateWithWStringAndRelease(unichar_t *wstring, MCStringRef& r_string);

libfoundation/src/foundation-string.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,50 @@ bool MCStringCreateWithCharsAndRelease(unichar_t *p_chars, uindex_t p_char_count
557557
return false;
558558
}
559559

560+
MC_DLLEXPORT_DEF
561+
bool MCStringCreateUnicodeWithChars(const unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
562+
{
563+
if (p_char_count == 0)
564+
{
565+
if (kMCEmptyString != nil)
566+
{
567+
r_string = MCValueRetain(kMCEmptyString);
568+
return true;
569+
}
570+
}
571+
else
572+
{
573+
MCAssert(nil != p_chars);
574+
}
575+
576+
bool t_success;
577+
t_success = true;
578+
579+
__MCString *self;
580+
self = nil;
581+
if (t_success)
582+
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
583+
584+
if (t_success)
585+
t_success = MCMemoryNewArray(p_char_count + 1, self -> chars);
586+
587+
if (t_success)
588+
{
589+
MCStrCharsMapFromUnicode(p_chars, p_char_count, self -> chars, self -> char_count);
590+
self -> flags |= kMCStringFlagIsNotNative;
591+
r_string = self;
592+
}
593+
else
594+
{
595+
if (self != nil)
596+
MCMemoryDeleteArray(self -> chars);
597+
598+
MCMemoryDelete(self);
599+
}
600+
601+
return t_success;
602+
}
603+
560604
MC_DLLEXPORT_DEF
561605
bool MCStringCreateWithWString(const unichar_t *p_wstring, MCStringRef& r_string)
562606
{

0 commit comments

Comments
 (0)