www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.base64 question

reply "Parke" <parke.nexus gmail.com> writes:
Hi,

The following does not compile, and I do not understand what I am 
doing wrong.

import std.base64;
ubyte[]  s0 = ['H','e','l','l','o'];
char[40] s1;
void main () {
   Base64.encode (s0, s1);
}

The compile error is:

[snip]/src/phobos/std/range.d(614): Error: static assert  "Cannot 
put a immutable(char) into a char[40]"
[snip]/src/phobos/std/base64.d(297):        instantiated from 
here: put!(char[40], immutable(char))
test.d(7):        instantiated from here: encode!(ubyte[], 
char[40])

Thanks!
Nov 26 2013
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 27 November 2013 at 02:43:56 UTC, Parke wrote:
 [snip]/src/phobos/std/range.d(614): Error: static assert  
 "Cannot put a immutable(char) into a char[40]"
Static arrays aren't considered ranges in templates because their length can't change. If you slice it, however, it will work, since a slice is a valid range: import std.base64; ubyte[] s0 = ['H','e','l','l','o']; char[40] s1; void main () { import std.stdio; auto encodedData = Base64.encode (s0, s1[]); // notice the [] writeln(encodedData); } Then base64.encode returns the slice into the string that it actually used. The slice operator, [], returns a view into the array that can be advanced by the encode function as needed.
Nov 26 2013
parent reply "Parke" <parke.nexus gmail.com> writes:
On Wednesday, 27 November 2013 at 02:49:19 UTC, Adam D. Ruppe 
wrote:
 The slice operator, [], returns a view into the array that can 
 be advanced by the encode function as needed.
Thanks. Are slices passed (and returned) by value or by reference?
Nov 26 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 27 November 2013 at 03:14:38 UTC, Parke wrote:
 Are slices passed (and returned) by value or by reference?
By value, though they are a pointer into the data. void foo(int[] data) { data[0] = 20; data ~= 100; } void main() { int[4] buffer; foo(buffer[]); // slice passed by value, but it still points into the buffer assert(buffer[0] == 20); // so the change to the data is reflected here assert(buffer.length == 4); // but the append is not visible here, since that affected the slice itself, passed by value, not the contents it pointed to. }
Nov 26 2013
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 27 November 2013 at 02:43:56 UTC, Parke wrote:
 The compile error is:

 [snip]/src/phobos/std/range.d(614): Error: static assert  
 "Cannot put a immutable(char) into a char[40]"
 [snip]/src/phobos/std/base64.d(297):        instantiated from 
 here: put!(char[40], immutable(char))
 test.d(7):        instantiated from here: encode!(ubyte[], 
 char[40])

 Thanks!
I'm not finding a solution, this specific error is because a static array isn't an output range, see example below for "fix." import std.range; void main () { immutable(ubyte) s0 = 5; ubyte[1] s1; auto slice = s1[]; slice.put(s0); } But fixing this just results in not finding a match due to template constraints somewhere: import std.base64; ubyte[] s0 = ['H','e','l','l','o']; char[40] s1; void main () { auto slice = s1[]; Base64.encode (slice, s1); } base64.d(6): Error: template std.base64.Base64Impl!('+', '/').encode does not match any function template declaration. Candidates are:...
Nov 26 2013