wchar[] test = "hello þ world"w.dup;
toUpperInPlace(test[6..7]);
assert(test == "hello Þ world");
The unicode letter Thorn is not converted in place.
Comment #1 by andrej.mitrovich — 2013-03-01T20:24:24Z
(In reply to comment #0)
> wchar[] test = "hello þ world"w.dup;
> toUpperInPlace(test[6..7]);
> assert(test == "hello Þ world");
>
> The unicode letter Thorn is not converted in place.
I'm noticing something strange:
import std.string;
import std.stdio;
void main()
{
wchar[] test = "abþdefg"w.dup;
toUpperInPlace(test[0 .. $]);
writeln(test);
}
writes: ABþdefg
Note how it stops uppercasing after that character.
Comment #2 by andrej.mitrovich — 2013-03-04T20:55:59Z
At a glance, it looks to me like the problem is this line:
s = s[0 .. i] ~ toAdd ~ s[j .. $];
See, it's not overwriting any memory, it's allocating and writing into new memory... that contradicts the 'InPlace' specification.
Shouldn't that line be more like:
s[i .. j] = toAdd[];
And I don't think there's any reason for the function to receive a 'ref'.
Comment #4 by andrej.mitrovich — 2013-03-04T23:51:22Z
(In reply to comment #3)
> At a glance, it looks to me like the problem is this line:
>
> s = s[0 .. i] ~ toAdd ~ s[j .. $];
>
> See, it's not overwriting any memory, it's allocating and writing into new
> memory... that contradicts the 'InPlace' specification.
>
> Shouldn't that line be more like:
> s[i .. j] = toAdd[];
>
> And I don't think there's any reason for the function to receive a 'ref'.
Hmm yeah that's the problem. It's kind of odd that a slice which only really lives inside the function is allowed to be passed by ref. What I mean is:
void foo(ref int[] a) { }
int[] a = [1, 2];
foo(a[0..2]);
It seems like this kind of slice should be treated as an rvalue, because ref semantics make no sense in this case as they won't propagate to the call site.
Comment #5 by andrej.mitrovich — 2013-03-06T05:51:40Z
(In reply to comment #4)
> It seems like this kind of slice should be treated as an rvalue, because ref
> semantics make no sense in this case as they won't propagate to the call site.
See also Kenji's comment about slices: https://github.com/D-Programming-Language/dmd/pull/1343#issuecomment-14482830
@Kenji: Is there a bug opened for slices not being rvalues?
Comment #6 by monarchdodra — 2013-05-30T02:17:28Z
(In reply to comment #0)
> wchar[] test = "hello þ world"w.dup;
> toUpperInPlace(test[6..7]);
> assert(test == "hello Þ world");
>
> The unicode letter Thorn is not converted in place.
Apart from the fact that currently, toUpperInPlace is not actually InPlace, which means this code does not work, and the whole r-value issue thingy, there is something dreadfully wrong with this code:
UTF simply does not work this way, as the upper or lower of a string may not have the same length as the original string.
Under this situation, doing what you are doing is aboslutly unsafe, and may end up either not working (forced relocation), or filling the string with garbage.
--------
I was under the impression that this bug was mostly about the codegen issue, so I did a new entry specifically for fixing the implementation of toUpperInPlace.
You can read in more details the issue there:
http://d.puremagic.com/issues/show_bug.cgi?id=10203
Comment #7 by monarchdodra — 2013-09-27T07:11:31Z
This was fixed in the new std.uni, and a regression introduced was also just fixed. So closing.