GoLang chunk_split

is this article helpful? yes | no
GoLang replacement for PHP's chunk_split [edit | history]
func ChunkSplit(body string, chunklen uint, end string) string {
	if end == "" {
		end = "\r\n"
	}
	runes, erunes := []rune(body), []rune(end)
	l := uint(len(runes))
	if l <= 1 || l < chunklen {
		return body + end
	}
	ns := make([]rune, 0, len(runes)+len(erunes))
	var i uint
	for i = 0; i < l; i += chunklen {
		if i+chunklen > l {
			ns = append(ns, runes[i:]...)
		} else {
			ns = append(ns, runes[i:i+chunklen]...)
		}
		ns = append(ns, erunes...)
	}
	return string(ns)
}

PHP chunk_split

PHP original manual for chunk_split [ show | php.net ]

chunk_split

(PHP 4, PHP 5, PHP 7)

chunk_splitSplit a string into smaller chunks

Description

string chunk_split ( string $body [, int $chunklen = 76 [, string $end = "\r\n" ]] )

Can be used to split a string into smaller chunks which is useful for e.g. converting base64_encode() output to match RFC 2045 semantics. It inserts end every chunklen characters.

Parameters

body

The string to be chunked.

chunklen

The chunk length.

end

The line ending sequence.

Return Values

Returns the chunked string.

Examples

Example #1 chunk_split() example

<?php
// format $data using RFC 2045 semantics
$new_string chunk_split(base64_encode($data));
?>

See Also