Bug 10162 – Opposite of std.string.representation

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-24T05:21:00Z
Last change time
2014-07-19T12:46:39Z
Keywords
preapproved
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-05-24T05:21:01Z
I suggest to add to Phobos a simple (unsafe) function that does the opposite of std.string.representation. An use case is for sorting an array of chars. sort() can't be used on a char[], so you have to conver it to ubyte[] with "representation", but later often you want an array of chars again): char[] word = ...; immutable key = word.representation.sort().release.unrepresentation.assumeUnique; Or if you start with a word: string word = ...; immutable key = word.dup.representation.sort().release.unrepresentation.assumeUnique; Such "unrepresentation" function is not safe. If you want to make it a bit safer its post-condition can run std.utf.validate on the result in non-release mode.
Comment #1 by bearophile_hugs — 2013-07-26T10:34:57Z
An alternative name for such function is "assumeChars". And it should return char[], const(char)[] or immutable(char)[] according to the input being a ubyte[], const(ubyte)[] or immutable(ubyte)[].
Comment #2 by bearophile_hugs — 2013-07-26T10:36:22Z
(In reply to comment #1) It could also return wchar[], const(wchar)[], immutable(wchar)[] according to the input being a ushort[], const(ushort)[] or immutable(ushort)[].
Comment #3 by bearophile_hugs — 2013-09-03T09:41:37Z
Most of my use cases are to sort a char[]. It's a common operation.
Comment #4 by bearophile_hugs — 2013-10-30T12:42:15Z
See also Issue 11356
Comment #5 by bearophile_hugs — 2013-11-13T02:00:59Z
(In reply to comment #3) > Most of my use cases are to sort a char[]. It's a common operation. So instead of unrepresentation an alternative solution is to introduce a little std.ascii.asciiSort (it could also be named just std.ascii.sort, or std.algorithm.asciiSort): char[] s1 = ['t', 'e', 's', 't']; string t1 = s1.asciiSort; string s2 = "test"; string t2 = s2.dup.asciiSort.assumeUnique;
Comment #6 by andrei — 2014-03-09T10:40:15Z
I think a function called assumeUTF would be nice. It would convert arrays of (possibly qualified) ubyte, ushort, and uint to the respective arrays of char, wchar, or dchar.
Comment #7 by github-bugzilla — 2014-07-19T12:45:08Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/133687cf7bb95bab9bfc80ca5e2f389ee6f1098e assumeUTF from idea of issue10162 format whitespace doc unittest assumeUTF with const immu validate in debug mode whitespace wrong attributes https://github.com/D-Programming-Language/phobos/commit/e119dad7f571d00692770479f90ef117edaa3a26 Merge pull request #2341 from burner/issue10162 Fix #10162: assumeUTF from idea