View source code
Display the source code in std/base64.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.base64.base64_impl.encode
- multiple declarations
- Function Base64Impl.encode
- Function Base64Impl.encode
- Function Base64Impl.encode
Function Base64Impl.encode
Encode source
into a char[]
buffer
using Base64
encoding.
Prototypes
char[] encode(R1, R2)( R1 source, R2 buffer ) pure @trusted if (isArray!R1 && is(ElementType!R1 : ubyte) && is(R2 == char[])); char[] encode(R1, R2)( R1 source, R2 buffer ) if (!isArray!R1 && isInputRange!R1 && is(ElementType!R1 : ubyte) && hasLength!R1 && is(R2 == char[]));
Parameters
Name | Description |
---|---|
source | The input range to encode. |
buffer | The char[] buffer to store the encoded result. |
Returns
The slice of buffer
that contains the encoded string.
Example
ubyte[] data = [0x83, 0xd7, 0x30, 0x7a, 0x01, 0x3f]; char[32] buffer; // much bigger than necessary // Just to be sure... auto encodedLength = Base64.encodeLength(data.length); assert(buffer.length >= encodedLength); // encode() returns a slice to the provided buffer. auto encoded = Base64.encode(data, buffer[]); assert(encoded is buffer[0 .. encodedLength]); assert(encoded == "g9cwegE/");
Function Base64Impl.encode
Encodes source
into an
output range
using
Base64
encoding.
Prototypes
size_t encode(R1, R2)( R1 source, R2 range ) if (isArray!R1 && is(ElementType!R1 : ubyte) && !is(R2 == char[]) && isOutputRange!(R2, char)); size_t encode(R1, R2)( R1 source, R2 range ) if (!isArray!R1 && isInputRange!R1 && is(ElementType!R1 : ubyte) && hasLength!R1 && !is(R2 == char[]) && isOutputRange!(R2, char));
Parameters
Name | Description |
---|---|
source | The input
range to encode. |
range | The output
range to store the encoded result. |
Returns
The number of times the output range
's put
method was invoked.
Example
struct OutputRange { char[] result; void put(const(char) ch) { result ~= ch; } } ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]; // This overload of encode() returns the number of calls to the output // range's put method. OutputRange output; assert(Base64.encode(data, output) == 8); assert(output.result == "Gis8TV1u");
Function Base64Impl.encode
Encodes source
to newly-allocated buffer.
This convenience method alleviates the need to manually manage output buffers.
Prototypes
char[] encode(Range)( Range source ) pure @safe if (isArray!Range && is(ElementType!Range : ubyte)); char[] encode(Range)( Range source ) if (!isArray!Range && isInputRange!Range && is(ElementType!Range : ubyte) && hasLength!Range);
Parameters
Name | Description |
---|---|
source | The input range to encode. |
Returns
A newly-allocated char[]
buffer containing the encoded string.
Example
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e]; assert(Base64.encode(data) == "Gis8TV1u");
Authors
Masahiro Nakagawa, Daniel Murphy (Single value Encoder and Decoder)