View source code
Display the source code in std/base64.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

std.base64.Base64Impl.encode - multiple declarations

Function Base64Impl.encode

Encode source into a char[] buffer using Base64 encoding.

char[] encode(R1, R2) (
  in 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

NameDescription
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]);
writeln(encoded); // "g9cwegE/"

Function Base64Impl.encode

Encodes source into an output range using Base64 encoding.

size_t encode(E, R) (
  scope const(E)[] source,
  auto ref R range
)
if (is(E : ubyte) && isOutputRange!(R, char) && !is(R == char[]));

size_t encode(R1, R2) (
  R1 source,
  auto ref R2 range
)
if (!isArray!R1 && isInputRange!R1 && is(ElementType!R1 : ubyte) && hasLength!R1 && !is(R2 == char[]) && isOutputRange!(R2, char));

Parameters

NameDescription
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

import std.array : appender;

auto output = appender!string();
ubyte[] data = [0x1a, 0x2b, 0x3c, 0x4d, 0x5d, 0x6e];

// This overload of encode() returns the number of calls to the output
// range's put method.
writeln(Base64.encode(data, output)); // 8
writeln(output.data); // "Gis8TV1u"

Function Base64Impl.encode

Encodes source to newly-allocated buffer.

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);

This convenience method alleviates the need to manually manage output buffers.

Parameters

NameDescription
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];
writeln(Base64.encode(data)); // "Gis8TV1u"

Authors

Masahiro Nakagawa, Daniel Murphy (Single value Encoder and Decoder)

License

Boost License 1.0.