Note that there are some explanatory texts on larger screens.

plurals
  1. POAssignment operator that calls a constructor is broken
    primarykey
    data
    text
    <p>I've implemented some of the changes suggested in <a href="https://stackoverflow.com/questions/2752240/why-cant-i-assign-a-scalar-value-to-a-class-using-shorthand-but-instead-declare">this question</a>, and (thanks very much) it works quite well, however... in the process I've seemed to break the post-declaration assignment operator. With the following code:</p> <pre><code>#include &lt;cstdio&gt; #include "ucpp" main() { ustring a = "test"; ustring b = "ing"; ustring c = "- -"; ustring d = "cafe\xcc\x81"; printf("%s\n", (a + b + c[1] + d).encode()); } </code></pre> <p>I get a nice "testing café" message. However, if I modify the code slightly so that the const char * conversion is done separately, post-declaration:</p> <pre><code>#include &lt;cstdio&gt; #include "ucpp" main() { ustring a = "test"; ustring b = "ing"; ustring c = "- -"; ustring d; d = "cafe\xcc\x81"; printf("%s\n", (a + b + c[1] + d).encode()); } </code></pre> <p>the ustring named d becomes blank, and all that is output is "testing ". My new code has three constructors, one void (which is probably the one being incorrectly used, and is used in the operator+ function), one that takes a const ustring &amp;, and one that takes a const char *. The following is my new library code:</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;cstring&gt; class ustring { int * values; long len; public: long length() { return len; } ustring() { len = 0; values = (int *) malloc(0); } ustring(const ustring &amp;input) { len = input.len; values = (int *) malloc(sizeof(int) * len); for (long i = 0; i &lt; len; i++) values[i] = input.values[i]; } ustring operator=(ustring input) { ustring result(input); return result; } ustring(const char * 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; input[i]; i++) if (input[i] &lt; 0x80) { // ASCII, direct copy (00-7f) values = (int *) realloc(values, sizeof(int) * ++s); values[s - 1] = input[i]; } else if (input[i] &lt; 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] &amp; 0x3f) &lt;&lt; ((contNeed - cont) * 6)); if (cont == contNeed) cont = contNeed = 0; } else if (input[i] &lt; 0xc2) { // invalid byte, use U+fffd (c0-c1) values = (int *) realloc(values, sizeof(int) * ++s); values[s - 1] = 0xfffd; } else if (input[i] &lt; 0xe0) { // start of 2-byte sequence (c2-df) contNeed = 1; values = (int *) realloc(values, sizeof(int) * ++s); values[s - 1] = (input[i] &amp; 0x1f) &lt;&lt; 6; } else if (input[i] &lt; 0xf0) { // start of 3-byte sequence (e0-ef) contNeed = 2; values = (int *) realloc(values, sizeof(int) * ++s); values[s - 1] = (input[i] &amp; 0x0f) &lt;&lt; 12; } else if (input[i] &lt; 0xf5) { // start of 4-byte sequence (f0-f4) contNeed = 3; values = (int *) realloc(values, sizeof(int) * ++s); values[s - 1] = (input[i] &amp; 0x07) &lt;&lt; 18; } else { // restricted or invalid (f5-ff) values = (int *) realloc(values, sizeof(int) * ++s); values[s - 1] = 0xfffd; } len = s; } ustring operator=(const char * input) { ustring result(input); return result; } ustring operator+(ustring input) { ustring result; result.len = len + input.len; result.values = (int *) malloc(sizeof(int) * result.len); for (long i = 0; i &lt; len; i++) result.values[i] = values[i]; for (long i = 0; i &lt; 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 &lt; len; i++) { if (values[i] &lt; 0x80) r = (char *) realloc(r, s + 1), r[s + 0] = char(values[i]), s += 1; else if (values[i] &lt; 0x800) r = (char *) realloc(r, s + 2), r[s + 0] = char(values[i] &gt;&gt; 6 | 0x60), r[s + 1] = char(values[i] &amp; 0x3f | 0x80), s += 2; else if (values[i] &lt; 0x10000) r = (char *) realloc(r, s + 3), r[s + 0] = char(values[i] &gt;&gt; 12 | 0xe0), r[s + 1] = char(values[i] &gt;&gt; 6 &amp; 0x3f | 0x80), r[s + 2] = char(values[i] &amp; 0x3f | 0x80), s += 3; else r = (char *) realloc(r, s + 4), r[s + 0] = char(values[i] &gt;&gt; 18 | 0xf0), r[s + 1] = char(values[i] &gt;&gt; 12 &amp; 0x3f | 0x80), r[s + 2] = char(values[i] &gt;&gt; 6 &amp; 0x3f | 0x80), r[s + 3] = char(values[i] &amp; 0x3f | 0x80), s += 4; } return r; } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload