mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: fix remaining mutability errors
This commit is contained in:
@ -25,25 +25,25 @@ const (
|
||||
*/
|
||||
pub fn decode(data string) string {
|
||||
buffer := malloc( data.len * 3 / 4 )
|
||||
return tos(buffer, decode_in_buffer(data, mut buffer) )
|
||||
return tos(buffer, decode_in_buffer(data, buffer) )
|
||||
}
|
||||
|
||||
/**
|
||||
* decode - expects a string. Returns its base64 encoded version.
|
||||
* @param data - the input string.
|
||||
* @return the base64 encoded version of the input string.
|
||||
* @return the base64 encoded version of the input string.
|
||||
* NB: base64 encoding returns a string that is ~ 4/3 larger than the input.
|
||||
* NB: if you need to encode many strings repeatedly, take a look at encode_in_buffer too.
|
||||
*/
|
||||
pub fn encode(data string) string {
|
||||
buffer := malloc( 4 * ((data.len + 2) / 3) )
|
||||
return tos(buffer, encode_in_buffer(data, mut buffer))
|
||||
return tos(buffer, encode_in_buffer(data, buffer))
|
||||
}
|
||||
|
||||
/**
|
||||
* decode_in_buffer - expects a string reference, and a buffer in which to store its decoded version.
|
||||
* @param data - a reference/pointer to the input string that will be decoded.
|
||||
* @param buffer - a reference/pointer to the buffer that will hold the result.
|
||||
* @param buffer - a reference/pointer to the buffer that will hold the result.
|
||||
* The buffer should be large enough (i.e. 3/4 of the data.len, or larger) to hold the decoded data.
|
||||
* @return the actual size of the decoded data in the buffer.
|
||||
* NB: this function does NOT allocate new memory, and is suitable for handling very large strings.
|
||||
@ -104,12 +104,12 @@ pub fn decode_in_buffer(data &string, buffer byteptr) int {
|
||||
/**
|
||||
* encode_in_buffer - expects a string reference, and a buffer in which to store its base64 encoded version.
|
||||
* @param data - a reference/pointer to the input string.
|
||||
* @param buffer - a reference/pointer to the buffer that will hold the result.
|
||||
* @param buffer - a reference/pointer to the buffer that will hold the result.
|
||||
* The buffer should be large enough (i.e. 4/3 of the data.len, or larger) to hold the encoded data.
|
||||
* @return the actual size of the encoded data in the buffer.
|
||||
* NB: this function does NOT allocate new memory, and is suitable for handling very large strings.
|
||||
*/
|
||||
pub fn encode_in_buffer(data &string, buffer mut byteptr) int {
|
||||
pub fn encode_in_buffer(data &string, buffer byteptr) int {
|
||||
input_length := data.len
|
||||
output_length := 4 * ((input_length + 2) / 3)
|
||||
|
||||
|
@ -3,26 +3,26 @@ import encoding.base64
|
||||
fn test_long_encoding(){
|
||||
repeats := 1000
|
||||
input_size := 3000
|
||||
|
||||
|
||||
s_original := 'a'.repeat(input_size)
|
||||
s_encoded := base64.encode(s_original)
|
||||
s_decoded := base64.decode(s_encoded)
|
||||
|
||||
|
||||
assert s_encoded.len > s_original.len
|
||||
assert s_original == s_decoded
|
||||
|
||||
mut s := 0
|
||||
|
||||
|
||||
ebuffer := malloc( s_encoded.len )
|
||||
for i := 0; i < repeats; i++ {
|
||||
resultsize := base64.encode_in_buffer(s_original, mut ebuffer)
|
||||
resultsize := base64.encode_in_buffer(s_original, ebuffer)
|
||||
s += resultsize
|
||||
assert resultsize == s_encoded.len
|
||||
}
|
||||
|
||||
dbuffer := malloc( s_decoded.len )
|
||||
for i := 0; i < repeats; i++ {
|
||||
resultsize := base64.decode_in_buffer(s_encoded, mut dbuffer)
|
||||
resultsize := base64.decode_in_buffer(s_encoded, dbuffer)
|
||||
s += resultsize
|
||||
assert resultsize == s_decoded.len
|
||||
}
|
||||
|
Reference in New Issue
Block a user