Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 65ac6d1

Browse files
committed
[[ Unicodify ]] Naive unicode string MCStringRef implementation.
1 parent e641ac2 commit 65ac6d1

File tree

8 files changed

+732
-219
lines changed

8 files changed

+732
-219
lines changed

engine/src/execpt.cpp

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -234,35 +234,43 @@ void MCExecPoint::grabbuffer(char *p_buffer, uindex_t p_length)
234234

235235
bool MCExecPoint::reserve(uindex_t p_capacity, char*& r_buffer)
236236
{
237-
MCStringRef t_string;
238-
if (!MCStringCreateMutable(p_capacity, t_string))
237+
MCDataRef t_string;
238+
if (!MCDataCreateMutable(p_capacity, t_string))
239239
return false;
240240

241+
MCDataPad(t_string, 0, p_capacity);
242+
241243
MCValueRelease(value);
242244
value = t_string;
243-
r_buffer = (char *)MCStringGetNativeCharPtr(t_string);
245+
r_buffer = (char *)MCDataGetBytePtr(t_string);
244246
return true;
245247
}
246248

247249
void MCExecPoint::commit(uindex_t p_size)
248250
{
249-
MCStringRemove((MCStringRef)value, MCRangeMake(p_size, UINDEX_MAX));
251+
MCDataRemove((MCDataRef)value, MCRangeMake(p_size, UINDEX_MAX));
250252
}
251253

252-
bool MCExecPoint::modify(char*& r_buffer, uindex_t& r_length)
254+
/*bool MCExecPoint::modify(char*& r_buffer, uindex_t& r_length)
253255
{
254-
converttomutablestring();
256+
converttostring();
257+
258+
MCDataRef t_data;
259+
MCDataCreateWithBytes(MCStringGetNativeCharPtr((MCStringRef)value), MCStringGetLength((MCStringRef)value), t_data);
255260
256-
r_buffer = (char *)MCStringGetNativeCharPtr((MCStringRef)value);
257-
r_length = MCStringGetLength((MCStringRef)value);
261+
MCValueRelease(value);
262+
MCDataMutableCopyAndRelease(t_data, (MCDataRef&)value);
263+
264+
r_buffer = (char *)MCDataGetBytePtr((MCDataRef)value);
265+
r_length = MCDataGetLength((MCDataRef)value);
258266
259267
return true;
260268
}
261269
262270
void MCExecPoint::resize(uindex_t p_size)
263271
{
264-
MCStringRemove((MCStringRef)value, MCRangeMake(p_size, UINDEX_MAX));
265-
}
272+
MCDataRemove((MCDataRef)value, MCRangeMake(p_size, UINDEX_MAX));
273+
}*/
266274

267275
//////////
268276

@@ -456,22 +464,6 @@ Exec_stat MCExecPoint::ston(void)
456464
return ES_NORMAL;
457465
}
458466

459-
void MCExecPoint::lower(void)
460-
{
461-
char *s;
462-
uint32_t l;
463-
modify(s, l);
464-
MCU_lower(s, MCString(s, l));
465-
}
466-
467-
void MCExecPoint::upper(void)
468-
{
469-
char *s;
470-
uint32_t l;
471-
modify(s, l);
472-
MCU_upper(s, MCString(s, l));
473-
}
474-
475467
// MW-2007-07-03: [[ Bug 5123 ]] - Strict array checking modification
476468
Exec_stat MCExecPoint::setitemdel(uint2 l, uint2 p)
477469
{
@@ -633,9 +625,15 @@ void MCExecPoint::concat(int4 n, Exec_concat ec, Boolean first)
633625

634626
void MCExecPoint::texttobinary(void)
635627
{
628+
MCDataRef t_data;
629+
copyasdataref(t_data);
630+
631+
MCDataMutableCopyAndRelease(t_data, t_data);
632+
636633
char *s;
637634
uint32_t l;
638-
modify(s, l);
635+
s = (char *)MCDataGetBytePtr(t_data);
636+
l = MCDataGetLength(t_data);
639637

640638
char *sptr, *dptr, *eptr;
641639
sptr = s;
@@ -662,7 +660,9 @@ void MCExecPoint::texttobinary(void)
662660
*dptr++ = *sptr++;
663661
}
664662

665-
resize(l);
663+
MCDataRemove(t_data, MCRangeMake(l, UINDEX_MAX));
664+
665+
setvalueref(t_data);
666666
}
667667

668668
void MCExecPoint::binarytotext(void)
@@ -695,15 +695,22 @@ void MCExecPoint::binarytotext(void)
695695
}
696696
}
697697
#elif defined(__CR__)
698+
MCDataRef t_data;
699+
copyasdataref(t_data);
700+
701+
MCDataMutableCopyAndRelease(t_data, t_data);
702+
698703
char *sptr;
699704
uint32_t l;
700-
modify(sptr, l);
705+
sptr = (char *)MCDataGetBytePtr(t_data);
706+
l = MCDataGetLength(t_data);
701707
for (uint32_t i = 0 ; i < l ; i++)
702708
{
703709
if (*sptr == '\n')
704710
*sptr = '\r';
705711
sptr++;
706712
}
713+
setvalueref(t_data);
707714
#endif
708715
}
709716

@@ -1025,18 +1032,6 @@ void MCExecPoint::concatreal(double p_value, Exec_concat p_sep, bool p_first)
10251032
delete[] t_buffer;
10261033
}
10271034

1028-
/////////
1029-
1030-
void MCExecPoint::replacechar(char p_from, char p_to)
1031-
{
1032-
char *s;
1033-
uint32_t l;
1034-
modify(s, l);
1035-
for(uint32_t i = 0; i < l; i++)
1036-
if (s[i] == p_from)
1037-
s[i] = p_to;
1038-
}
1039-
10401035
////////////////////////////////////////////////////////////////////////////////
10411036

10421037
bool MCExecPoint::copyasbool(bool& r_value)

engine/src/typedefs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1919

2020
// IEEE floating-point limits
2121

22+
#ifndef DBL_DIG
2223
#define DBL_DIG 15 /* # of decimal digits of precision */
2324
#define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
2425
#define DBL_MANT_DIG 53 /* # of bits in mantissa */
@@ -28,9 +29,12 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
2829
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */
2930
#define DBL_MIN_10_EXP (-307) /* min decimal exponent */
3031
#define DBL_MIN_EXP (-1021) /* min binary exponent */
32+
#endif
33+
3134
#define _DBL_RADIX 2 /* exponent radix */
3235
#define _DBL_ROUNDS 1 /* addition rounding: near */
3336

37+
#ifndef FLT_DIG
3438
#define FLT_DIG 6 /* # of decimal digits of precision */
3539
#define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */
3640
#define FLT_GUARD 0
@@ -44,6 +48,7 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
4448
#define FLT_NORMALIZE 0
4549
#define FLT_RADIX 2 /* exponent radix */
4650
#define FLT_ROUNDS 1 /* addition rounding: near */
51+
#endif
4752

4853
// Old-style integer definitions and limits
4954

libfoundation/include/foundation.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ extern MCNumberRef kMCMinusOne;
10781078

10791079
// Create a name using the given string.
10801080
bool MCNameCreate(MCStringRef string, MCNameRef& r_name);
1081+
// Create a name using chars.
1082+
bool MCNameCreateWithChars(const unichar_t *chars, uindex_t count, MCNameRef& r_name);
10811083
// Create a name using native chars.
10821084
bool MCNameCreateWithNativeChars(const char_t *chars, uindex_t count, MCNameRef& r_name);
10831085

@@ -1488,7 +1490,7 @@ bool MCStringPad(MCStringRef string, uindex_t at, uindex_t count, MCStringRef va
14881490
//
14891491
// Note that 'string' must be mutable.
14901492
bool MCStringFindAndReplace(MCStringRef string, MCStringRef pattern, MCStringRef replacement, MCStringOptions options);
1491-
bool MCStringFindAndReplaceChar(MCStringRef string, char_t pattern, char_t replacement, MCStringOptions options);
1493+
bool MCStringFindAndReplaceChar(MCStringRef string, codepoint_t pattern, codepoint_t replacement, MCStringOptions options);
14921494

14931495
/////////
14941496

@@ -1548,6 +1550,8 @@ bool MCDataInsert(MCDataRef r_data, uindex_t p_at, MCDataRef p_new_data);
15481550
bool MCDataRemove(MCDataRef r_data, MCRange p_range);
15491551
bool MCDataReplace(MCDataRef r_data, MCRange p_range, MCDataRef p_new_data);
15501552

1553+
bool MCDataPad(MCDataRef data, byte_t byte, uindex_t count);
1554+
15511555
////////////////////////////////////////////////////////////////////////////////
15521556
//
15531557
// ARRAY DEFINITIONS

libfoundation/src/foundation-data.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ bool MCDataReplace(MCDataRef r_data, MCRange p_range, MCDataRef p_new_data)
372372
return true;
373373
}
374374

375+
bool MCDataPad(MCDataRef p_data, byte_t p_byte, uindex_t p_count)
376+
{
377+
if (!__MCDataExpandAt(p_data, p_data -> byte_count, p_count))
378+
return false;
379+
380+
memset(p_data -> bytes + p_data -> byte_count - p_count, p_byte, p_count);
381+
return true;
382+
}
383+
375384
static void __MCDataClampRange(MCDataRef p_data, MCRange& x_range)
376385
{
377386
uindex_t t_left, t_right;

libfoundation/src/foundation-name.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ bool MCNameCreateWithNativeChars(const char_t *p_chars, uindex_t p_count, MCName
156156
return MCNameCreateAndRelease(t_string, r_name);
157157
}
158158

159+
bool MCNameCreateWithChars(const unichar_t *p_chars, uindex_t p_count, MCNameRef& r_name)
160+
{
161+
MCStringRef t_string;
162+
if (!MCStringCreateWithChars(p_chars, p_count, t_string))
163+
return false;
164+
return MCNameCreateAndRelease(t_string, r_name);
165+
}
166+
159167
bool MCNameCreateAndRelease(MCStringRef p_string, MCNameRef& r_name)
160168
{
161169
if (MCNameCreate(p_string, r_name))

libfoundation/src/foundation-private.h

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,72 @@ enum
7575
kMCStringFlagIsUnicode = 1 << 0,
7676
// If set then the string is mutable.
7777
kMCStringFlagIsMutable = 1 << 1,
78+
#ifndef NATIVE_STRING
79+
// If set then the native and unicode strings are equivalent
80+
kMCStringFlagIsNative = 1 << 2,
81+
#endif
7882
};
7983

84+
#ifdef NATIVE_STRING
85+
typedef char_t strchar_t;
86+
#define MCStrCharFold(x) MCNativeCharFold(x)
87+
#define MCStrCharLowercase(x) MCNativeCharLowercase(x)
88+
#define MCStrCharUppercase(x) MCNativeCharUppercase(x)
89+
#define MCStrCharMapToNative(x) (x)
90+
#define MCStrCharMapFromNative(x) (x)
91+
#define MCStrCharsMapFromUnicode(x, y, z, w) MCUnicodeCharsMapToNative(x, y, z, w, '?')
92+
#define MCStrCharsMapFromNative(x, y, z) MCMemoryCopy(x, y, z * sizeof(strchar_t))
93+
#define MCStrCharsHashExact(x, y) MCNativeCharsHashExact(x, y)
94+
#define MCStrCharsHashCaseless(x, y) MCNativeCharsHashCaseless(x, y)
95+
#define MCStrCharsEqualExact(x, y, z, w) MCNativeCharsEqualExact(x, y, z, w)
96+
#define MCStrCharsEqualCaseless(x, y, z, w) MCNativeCharsEqualCaseless(x, y, z, w)
97+
#define MCStrCharsCompareExact(x, y, z, w) MCNativeCharsCompareExact(x, y, z, w)
98+
#define MCStrCharsCompareCaseless(x, y, z, w) MCNativeCharsCompareCaseless(x, y, z, w)
99+
#define MCStrCharsSharedPrefixExact(x, y, z, w) MCNativeCharsSharedPrefixExact(x, y, z, w)
100+
#define MCStrCharsSharedPrefixCaseless(x, y, z, w) MCNativeCharsSharedPrefixCaseless(x, y, z, w)
101+
#define MCStrCharsSharedSuffixExact(x, y, z, w) MCNativeCharsSharedSuffixExact(x, y, z, w)
102+
#define MCStrCharsSharedSuffixCaseless(x, y, z, w) MCNativeCharsSharedSuffixCaseless(x, y, z, w)
103+
#define MCStrCharsLowercase(x, y) MCNativeCharsLowercase(x, y)
104+
#define MCStrCharsUppercase(x, y) MCNativeCharsUppercase(x, y)
105+
#else
106+
typedef unichar_t strchar_t;
107+
#define MCStrCharFold(x) MCUnicodeCharFold(x)
108+
#define MCStrCharLowercase(x) MCUnicodeCharLowercase(x)
109+
#define MCStrCharUppercase(x) MCUnicodeCharUppercase(x)
110+
#define MCStrCharMapToNative(x) MCUnicodeCharMapToNativeLossy(x)
111+
#define MCStrCharMapFromNative(x) MCUnicodeCharMapFromNative(x)
112+
#define MCStrCharsMapFromUnicode(x, y, z, w) (MCMemoryCopy(z, x, y * sizeof(strchar_t)), w = y)
113+
#define MCStrCharsMapFromNative(x, y, z) MCUnicodeCharsMapFromNative(y, z, x)
114+
#define MCStrCharsHashExact(x, y) MCUnicodeCharsHashExact(x, y)
115+
#define MCStrCharsHashCaseless(x, y) MCUnicodeCharsHashCaseless(x, y)
116+
#define MCStrCharsEqualExact(x, y, z, w) MCUnicodeCharsEqualExact(x, y, z, w)
117+
#define MCStrCharsEqualCaseless(x, y, z, w) MCUnicodeCharsEqualCaseless(x, y, z, w)
118+
#define MCStrCharsCompareExact(x, y, z, w) MCUnicodeCharsCompareExact(x, y, z, w)
119+
#define MCStrCharsCompareCaseless(x, y, z, w) MCUnicodeCharsCompareCaseless(x, y, z, w)
120+
#define MCStrCharsSharedPrefixExact(x, y, z, w) MCUnicodeCharsSharedPrefixExact(x, y, z, w)
121+
#define MCStrCharsSharedPrefixCaseless(x, y, z, w) MCUnicodeCharsSharedPrefixCaseless(x, y, z, w)
122+
#define MCStrCharsSharedSuffixExact(x, y, z, w) MCUnicodeCharsSharedSuffixExact(x, y, z, w)
123+
#define MCStrCharsSharedSuffixCaseless(x, y, z, w) MCUnicodeCharsSharedSuffixCaseless(x, y, z, w)
124+
#define MCStrCharsLowercase(x, y) MCUnicodeCharsLowercase(x, y)
125+
#define MCStrCharsUppercase(x, y) MCUnicodeCharsUppercase(x, y)
126+
#endif
127+
128+
#ifdef NATIVE_STRING
80129
struct __MCString: public __MCValue
81130
{
82131
uindex_t char_count;
83-
char_t *chars;
132+
strchar_t *chars;
84133
uindex_t capacity;
85134
};
135+
#else
136+
struct __MCString: public __MCValue
137+
{
138+
uindex_t char_count;
139+
strchar_t *chars;
140+
char_t *native_chars;
141+
uindex_t capacity;
142+
};
143+
#endif
86144

87145
//////////
88146

@@ -296,15 +354,44 @@ hash_t MCUnicodeCharsHashCaseless(const unichar_t *chars, uindex_t char_count);
296354
bool MCUnicodeCharsEqualExact(const unichar_t *left, uindex_t left_length, const unichar_t *right, uindex_t right_length);
297355
bool MCUnicodeCharsEqualCaseless(const unichar_t *left, uindex_t left_length, const unichar_t *right, uindex_t right_length);
298356

357+
compare_t MCUnicodeCharsCompareExact(const unichar_t *left, uindex_t left_length, const unichar_t *right, uindex_t right_length);
358+
compare_t MCUnicodeCharsCompareCaseless(const unichar_t *left, uindex_t left_length, const unichar_t *right, uindex_t right_length);
359+
360+
// Return the number of characters of prefix that are equal to those at the
361+
// beginning of string.
362+
uindex_t MCUnicodeCharsSharedPrefixExact(const unichar_t *string, uindex_t left_length, const unichar_t *suffix, uindex_t right_length);
363+
uindex_t MCUnicodeCharsSharedPrefixCaseless(const unichar_t *string, uindex_t left_length, const unichar_t *suffix, uindex_t right_length);
364+
365+
// Return the number of characters of suffix that are equal to those at the
366+
// end of string.
367+
uindex_t MCUnicodeCharsSharedSuffixExact(const unichar_t *string, uindex_t left_length, const unichar_t *suffix, uindex_t right_length);
368+
uindex_t MCUnicodeCharsSharedSuffixCaseless(const unichar_t *string, uindex_t left_length, const unichar_t *suffix, uindex_t right_length);
369+
370+
// Lowercase all the characters in-place.
371+
void MCUnicodeCharsLowercase(unichar_t *chars, uindex_t char_count);
372+
373+
// Uppercase all the characters in-place.
374+
void MCUnicodeCharsUppercase(unichar_t *chars, uindex_t char_count);
375+
376+
bool MCUnicodeCharsEqualExact(const unichar_t *left, uindex_t left_length, const unichar_t *right, uindex_t right_length);
377+
bool MCUnicodeCharsEqualCaseless(const unichar_t *left, uindex_t left_length, const unichar_t *right, uindex_t right_length);
378+
299379
bool MCUnicodeCharsMapToNative(const unichar_t *uchars, uindex_t uchar_count, char_t *nchars, uindex_t& r_nchar_count, char_t invalid);
300380
void MCUnicodeCharsMapFromNative(const char_t *chars, uindex_t char_count, unichar_t *uchars);
301381

302382
uindex_t MCUnicodeCharsMapToUTF8(const unichar_t *wchars, uindex_t wchar_count, byte_t *utf8bytes, uindex_t utf8byte_count);
303383
uindex_t MCUnicodeCharsMapFromUTF8(const byte_t *utf8bytes, uindex_t utf8byte_count, unichar_t *wchars, uindex_t wchar_count);
304384

305385
bool MCUnicodeCharMapToNative(unichar_t uchar, char_t& r_nchar);
386+
char_t MCUnicodeCharMapToNativeLossy(unichar_t nchar);
306387
unichar_t MCUnicodeCharMapFromNative(char_t nchar);
307388

389+
unichar_t MCUnicodeCharFold(unichar_t);
390+
391+
unichar_t MCUnicodeCharLowercase(unichar_t);
392+
393+
unichar_t MCUnicodeCharUppercase(unichar_t);
394+
308395
////////////////////////////////////////////////////////////////////////////////
309396

310397
#endif

0 commit comments

Comments
 (0)