2025-02-09 12:49:56 +00:00
|
|
|
#import "Foundation/NSString.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "GSPrivate.h"
|
2021-07-16 12:31:38 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_UNICODE_UTEXT_H)
|
2010-11-18 22:52:36 +00:00
|
|
|
#include <unicode/utext.h>
|
2021-08-06 07:29:11 +00:00
|
|
|
#elif defined(HAVE_ICU_H)
|
2021-07-16 12:31:38 +00:00
|
|
|
#include <icu.h>
|
|
|
|
// icu.h in Windows 10 is missing a declaration of UTEXT_MAGIC
|
|
|
|
#ifndef UTEXT_MAGIC
|
|
|
|
#define UTEXT_MAGIC 0x345ad82c
|
|
|
|
#endif
|
|
|
|
#endif
|
2010-11-18 22:52:36 +00:00
|
|
|
|
2020-11-12 16:18:09 +00:00
|
|
|
/*
|
|
|
|
* Define TRUE/FALSE to be used with UBool parameters, as these are no longer
|
|
|
|
* defined in ICU as of ICU 68.
|
|
|
|
*/
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
2010-11-18 22:52:36 +00:00
|
|
|
/**
|
|
|
|
* Initialises a UText structure with an NSString. If txt is NULL, then this
|
|
|
|
* allocates a new structure on the heap, otherwise it fills in the existing
|
|
|
|
* one.
|
|
|
|
*
|
|
|
|
* The returned UText object holds a reference to the NSString and accesses its
|
|
|
|
* contents directly.
|
|
|
|
*/
|
2025-02-09 12:49:56 +00:00
|
|
|
UText* UTextInitWithNSString(UText *txt, NSString *str) GS_ATTRIB_PRIVATE;
|
2011-02-11 14:31:25 +00:00
|
|
|
|
2010-11-18 22:52:36 +00:00
|
|
|
/**
|
|
|
|
* Initialises a UText structure with an NSMutableString. If txt is NULL, then
|
|
|
|
* this allocates a new structure on the heap, otherwise it fills in the
|
|
|
|
* existing one.
|
|
|
|
*
|
|
|
|
* The returned UText object holds a reference to the NSMutableString and
|
|
|
|
* accesses its contents directly.
|
|
|
|
*
|
|
|
|
* This function returns a mutable UText, and changes made to it will be
|
|
|
|
* reflected in the underlying NSMutableString.
|
|
|
|
*/
|
2025-02-09 12:49:56 +00:00
|
|
|
UText* UTextInitWithNSMutableString(UText *txt, NSMutableString *str)
|
|
|
|
GS_ATTRIB_PRIVATE;
|
2011-02-11 14:31:25 +00:00
|
|
|
|
2010-11-18 22:52:36 +00:00
|
|
|
/**
|
|
|
|
* GSUTextString is an NSString subclass that is backed by a libicu UText
|
|
|
|
* structure. This class is intended to be used when returning UText created
|
|
|
|
* by libicu functions to Objective-C code.
|
|
|
|
*/
|
|
|
|
@interface GSUTextString : NSString
|
|
|
|
{
|
2011-02-11 14:31:25 +00:00
|
|
|
@public
|
|
|
|
/** The UText structure containing the libicu string interface. */
|
|
|
|
UText txt;
|
2010-11-18 22:52:36 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GSUTextString is an NSMutableString subclass that is backed by a libicu
|
|
|
|
* UText structure. This class is intended to be used when returning UText
|
|
|
|
* created by libicu functions to Objective-C code.
|
|
|
|
*/
|
|
|
|
@interface GSUTextMutableString : NSMutableString
|
|
|
|
{
|
2011-02-11 14:31:25 +00:00
|
|
|
@public
|
|
|
|
/** The UText structure containing the libicu string interface. */
|
|
|
|
UText txt;
|
2010-11-18 22:52:36 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup function used to fee a unichar buffer.
|
|
|
|
*/
|
|
|
|
static inline void free_string(unichar **buf)
|
|
|
|
{
|
2011-02-11 14:31:25 +00:00
|
|
|
if (0 != *buf)
|
|
|
|
{
|
|
|
|
free(*buf);
|
|
|
|
}
|
2010-11-18 22:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a temporary buffer of the requested size. This allocates buffers
|
|
|
|
* of up to 64 bytes on the stack or more than 64 bytes on the heap. The
|
|
|
|
* buffer is automatically destroyed when it goes out of scope in either case.
|
|
|
|
*
|
|
|
|
* Buffers created in this way are exception safe when using native exceptions.
|
|
|
|
*/
|
2021-07-16 11:43:07 +00:00
|
|
|
#define TEMP_BUFFER(name, length)\
|
2011-02-11 14:31:25 +00:00
|
|
|
__attribute__((cleanup(free_string))) unichar *name ##_onheap = 0;\
|
2021-07-16 11:43:07 +00:00
|
|
|
unichar name ## _onstack[64];\
|
2011-02-11 14:31:25 +00:00
|
|
|
unichar *name = name ## _onstack;\
|
2021-07-16 11:43:07 +00:00
|
|
|
if (length > 64)\
|
2011-02-11 14:31:25 +00:00
|
|
|
{\
|
2021-07-16 11:43:07 +00:00
|
|
|
name ## _onheap = malloc(length * sizeof(unichar));\
|
2011-02-11 14:31:25 +00:00
|
|
|
name = name ## _onheap;\
|
|
|
|
}
|
2010-11-19 22:15:13 +00:00
|
|
|
|