0

I'm trying to copy a char16_t* that's passed into a function but haven't found any way to do that.

foo(char16_t* characters) {
    char16_t* copiedCharacters;
    //copy characters to copiedCharacters
}

I tried to use strncopy() but it only copies char*.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Lenny
  • 388
  • 3
  • 15

2 Answers2

2

There is a known construction (borrowed from strcpy):

char16_t* str16cpy(char16_t* destination, const char16_t* source)
{
    char16_t* temp = destination;
    while((*temp++ = *source++) != 0)
    ;
    return destination;
}
Loreto
  • 674
  • 6
  • 20
-1

Just adjust strcpy implementation

char16_t *mystrcpy(char16_t *dest, const char16_t *src)
{
  unsigned i;
  for (i=0; src[i] != '\0'; ++i)
    dest[i] = src[i];
  dest[i] = '\0';
  return dest;
}
Mateusz Drost
  • 1,171
  • 10
  • 23
  • 1
    if you use an index, the type should be `size_t`. – chqrlie Dec 13 '16 at 00:45
  • chqrlie: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf "7.17 Common definitions (...) size_t which is the unsigned integer type of the result of the sizeof operator" – Mateusz Drost Dec 13 '16 at 01:16
  • 2
    `size_t` is _some_ unsigned integer type. It is not a synonym for `unsigned`. example: `size_t` may be wider than `unsigned`. `size_t` is the best type to use for array indexing and size calculations - neither too narrow, nor too wide. – chux - Reinstate Monica Dec 13 '16 at 02:45
  • chux: Show me fragment in the standard about it. Also `size_t` is not the best type for array indexing and size calculation because of performance drop due to overflow behavior. Bjarne Stroustrup regrets that `size_t` is not signed. – Mateusz Drost Dec 13 '16 at 02:57
  • I think the part chux is refererring to is in N1570 $6.2.5 p6, which defines what what *unsigned integer type* means in the standard document. When standard refers to specific type, it explitly mentions it. This doesn't happen in the definition of the `size_t`. – user694733 Dec 13 '16 at 07:27
  • @MateuszDrost: *Show me fragment in the standard about it*: `size_t` is the type returned by `strlen()`, it is the type used for most object sizes and counts of objects (as in `fread`, `fwrite`, `malloc`, `calloc`, `qsort`, `bsearch`, `snprintf`... `int` might be too small to represent all valid offsets in arrays pointed to by `dest` and `src`. For example if `src` points to a very long (2G) string of `char16_t`, `mystrcpy` would invoke undefined behavior when `i` reaches `INT_MAX` both because of arithmetic overflow and because if would dereference a negative offset from `src`. – chqrlie Dec 13 '16 at 08:31
  • @MateuszDrost: similarly, `unsigned` would cause `mystrcpy` to loop forever if the source array has more than 4G entries. These values may seem large, but are possible today with current architectures. Architectures where `size_t` causes a performance drop vs `unsigned` for indexing should use a 32-bit ABI, on 64-bit systems using 32 bit index values require zero or sign extensions before the indexing operation can take place. – chqrlie Dec 13 '16 at 08:43
  • @MateuszDrost: Bjarne Stroustrup has many regrets about the C legacy, but it is a consequence of the initial compatibility goals. C++ is so far from C today nothing stops him from severing the remaining ties, except billions of lines of legacy code. – chqrlie Dec 13 '16 at 08:45