I am writing a UTF-8 library for C++ as an exercise as this is my first real-world C++ code. So far, I've implemented concatenation, character indexing, parsing and encoding UTF-8 in a class called "ustring". It looks like it's working, but two seemingly equivalent ways of declaring a new ustring behave differently. The first way:
ustring a;
a = "test";
works, and the overloaded "=" operator parses the string into the class (which stores the Unicode strings as an dynamically allocated int pointer). However, the following does not work:
ustring a = "test";
because I get the following error:
test.cpp:4: error: conversion from ‘const char [5]’ to non-scalar type ‘ustring’ requested
Is there a way to workaround this error? It probably is a problem with my code, though. The following is what I've written so far for the library:
#include <cstdlib>
#include <cstring>
class ustring {
int * values;
long len;
public:
long length() {
return len;
}
ustring * operator=(ustring input) {
len = input.len;
values = (int *) malloc(sizeof(int) * len);
for (long i = 0; i < len; i++)
values[i] = input.values[i];
return this;
}
ustring * operator=(char input[]) {
len = sizeof(input);
values = (int *) malloc(0);
long s = 0; // s = number of parsed chars
int a, b, c, d, contNeed = 0, cont = 0;
for (long i = 0; i < sizeof(input); i++)
if (input[i] < 0x80) { // ASCII, direct copy (00-7f)
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = input[i];
} else if (input[i] < 0xc0) { // this is a continuation (80-bf)
if (cont == contNeed) { // no need for continuation, use U+fffd
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = 0xfffd;
}
cont = cont + 1;
values[s - 1] = values[s - 1] | ((input[i] & 0x3f) << ((contNeed - cont) * 6));
if (cont == contNeed) cont = contNeed = 0;
} else if (input[i] < 0xc2) { // invalid byte, use U+fffd (c0-c1)
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = 0xfffd;
} else if (input[i] < 0xe0) { // start of 2-byte sequence (c2-df)
contNeed = 1;
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = (input[i] & 0x1f) << 6;
} else if (input[i] < 0xf0) { // start of 3-byte sequence (e0-ef)
contNeed = 2;
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = (input[i] & 0x0f) << 12;
} else if (input[i] < 0xf5) { // start of 4-byte sequence (f0-f4)
contNeed = 3;
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = (input[i] & 0x07) << 18;
} else { // restricted or invalid (f5-ff)
values = (int *) realloc(values, sizeof(int) * ++s);
values[s - 1] = 0xfffd;
}
return this;
}
ustring operator+(ustring input) {
ustring result;
result.len = len + input.len;
result.values = (int *) malloc(sizeof(int) * result.len);
for (long i = 0; i < len; i++)
result.values[i] = values[i];
for (long i = 0; i < input.len; i++)
result.values[i + len] = input.values[i];
return result;
}
ustring operator[](long index) {
ustring result;
result.len = 1;
result.values = (int *) malloc(sizeof(int));
result.values[0] = values[index];
return result;
}
char * encode() {
char * r = (char *) malloc(0);
long s = 0;
for (long i = 0; i < len; i++) {
if (values[i] < 0x80)
r = (char *) realloc(r, s + 1),
r[s + 0] = char(values[i]),
s += 1;
else if (values[i] < 0x800)
r = (char *) realloc(r, s + 2),
r[s + 0] = char(values[i] >> 6 | 0x60),
r[s + 1] = char(values[i] & 0x3f | 0x80),
s += 2;
else if (values[i] < 0x10000)
r = (char *) realloc(r, s + 3),
r[s + 0] = char(values[i] >> 12 | 0xe0),
r[s + 1] = char(values[i] >> 6 & 0x3f | 0x80),
r[s + 2] = char(values[i] & 0x3f | 0x80),
s += 3;
else
r = (char *) realloc(r, s + 4),
r[s + 0] = char(values[i] >> 18 | 0xf0),
r[s + 1] = char(values[i] >> 12 & 0x3f | 0x80),
r[s + 2] = char(values[i] >> 6 & 0x3f | 0x80),
r[s + 3] = char(values[i] & 0x3f | 0x80),
s += 4;
}
return r;
}
};
Your problem is that
ustring a = "test"
actually invokes the constructor, not the assignment operator. yay, welcome to c++ :)You'll need to define yourself both a default constructor and one that takes a
const char*
, because once you define a constructor, you need to define all your constructors.A few other things:
const char *
instead ofchar[]
(you don't modify the input and char* is more common)sizeof
isn't doing what you think it's doing, it doesn't work properly for array parameters. It is returning yousizeof(char*)
, notsizeof(array)
.this
from your operators.vector<int> values;
to manage all your memory for you.encode()
should probably return astring
. Withstring
:free
ordelete
it.s.append(c);
instead of usingrealloc
.printf("%s", s.c_str());
, but in c++ you usually usecout << s;
Like this:
If this is c++, why are you doing all this malloc/realloc stuff? I haven't fully parsed that code, but I'd imagine there's a simpler way... see the comment about using vector.
As @Michael Aaron Safyan mentioned in the comments, if you do any memory allocation for the
ustring
class, you will want to deallocate it in the destructor. However, I think by switching to memory managed containers - vector & string - you'll avoid any of your own memory management and can avoid writing a destructor.These are two operations:
This is one operation:
In the interest of runtime efficiency and allowing semantic freedom, C++ does not extrapolate the default constructor
ustring::ustring()
and the assignment operatorustring::operator=(const char*)
into a constructorustring::ustring(const char *)
.But, for most reasonable string classes, this will work:
It is better to call the assignment operator from the constructor than to attempt it the other way around.
Of course, you can probably improve efficiency by considering the length of the given string while performing initialization.