Skip to content

Commit a847165

Browse files
committed
[minor feature] Add u_memdup function
1 parent a445530 commit a847165

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Unicode.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4167,6 +4167,35 @@ if (res==NULL) {
41674167
return (unichar*)memcpy(res,str,buflen);
41684168
}
41694169

4170+
/**
4171+
* @brief Allocate a block of memory of a specific length then copy the contents of str
4172+
* into the allocated space
4173+
*
4174+
* @return A pointer to the newly allocated space.
4175+
*
4176+
* @note This function doesn't guarantee a null-terminated string, use u_strdup()
4177+
* instead
4178+
*
4179+
* @author Cristian Martinez
4180+
*/
4181+
unichar* u_memdup(const unichar* str, size_t len, Abstract_allocator prv_alloc) {
4182+
if (str == NULL) {
4183+
return NULL;
4184+
}
4185+
4186+
size_t bufflen = len * sizeof(unichar);
4187+
4188+
void* memptr = malloc_cb(bufflen, prv_alloc);
4189+
4190+
if (memptr == NULL) {
4191+
fatal_alloc_error("u_memdup");
4192+
}
4193+
4194+
memcpy(memptr, str, bufflen);
4195+
4196+
return reinterpret_cast<unichar *>(memptr);
4197+
}
4198+
41704199

41714200
/**
41724201
* This version has the correct prototype to be used as a keycopy function for
@@ -4291,7 +4320,7 @@ while (*s) {
42914320
return NULL;
42924321
}
42934322

4294-
4323+
#if !UNITEX_USE(BASE_UNICODE)
42954324
/**
42964325
* Unicode version of strchr.
42974326
* This function returns a pointer on the first occurrence of 'c' in 's', or
@@ -4310,6 +4339,7 @@ while ((*s)) {
43104339

43114340
return NULL;
43124341
}
4342+
#endif
43134343

43144344
unichar* u_strchr(unichar* s,unichar c) {
43154345
if (s==NULL) return NULL;

src/Unicode.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,17 @@ unichar *u_strtok_r(unichar *str, const unichar *delim, unichar **saveptr);
314314
typedef int (*EQUAL_UNICHAR_FUNCTION)(const unichar*,const unichar*);
315315
int u_equal(const unichar*, const unichar*);
316316
int u_equal_ignore_case(const unichar*, const unichar*);
317+
317318
unichar* u_strdup(const unichar*);
318319
unichar* u_strndup(const unichar*,int);
319320
unichar* u_strndup(const char*,int);
320321
unichar* keycopy(unichar*);
321322
unichar* u_strdup(const unichar*,unsigned int);
322323
unichar* u_strdup(const char*);
323324
const unichar* u_strchr(const unichar*,unichar,int);
325+
#if !UNITEX_USE(BASE_UNICODE)
324326
const unichar* u_strchr(const unichar*,unichar);
327+
#endif
325328
unichar* u_strchr(unichar*,unichar);
326329
int u_strrchr(const unichar*,unichar);
327330
int u_strrchr(const unichar*,char);
@@ -334,7 +337,7 @@ int u_ends_with(const unichar*,const unichar*);
334337
int u_ends_with(const unichar*,const char*);
335338
int u_substr(const unichar*,const unichar*);
336339

337-
340+
unichar* u_memdup(const unichar* str, size_t len, Abstract_allocator prv_alloc);
338341
unichar* u_strdup(const unichar* str,Abstract_allocator prv_alloc);
339342
unichar* u_strdup(const char* str,Abstract_allocator prv_alloc);
340343
unichar* u_strdup(const unichar* str,int n,Abstract_allocator prv_alloc);

0 commit comments

Comments
 (0)