vlib: fix remaining mutability errors

pull/3006/head
Alexander Medvednikov 2019-12-07 15:13:23 +03:00
parent 329485d4b6
commit c2814c1ada
3 changed files with 20 additions and 21 deletions

View File

@ -126,7 +126,7 @@ fn new_x11_clipboard(selection atom_type) &Clipboard {
if !(selection in [.clipboard, .primary, .secondary]) {
panic("Wrong atom_type. Must be one of .primary, .secondary or .clipboard.")
}
//init x11 thread support
status := XInitThreads()
if status == 0 {
@ -184,7 +184,6 @@ fn (cb &Clipboard) take_ownership(){
fn (cb mut Clipboard) set_text(text string) bool {
if cb.window == Window(C.None) {return false}
mut ret := false
cb.mutex.lock()
cb.text = text
cb.is_owner = true
@ -202,10 +201,10 @@ fn (cb mut Clipboard) get_text() string {
return cb.text
}
cb.got_text = false
//Request a list of possible conversions, if we're pasting.
XConvertSelection(cb.display, cb.selection, cb.get_atom(.targets), cb.selection, cb.window, C.CurrentTime)
//wait for the text to arrive
mut retries := 5
for {
@ -260,7 +259,7 @@ fn (cb mut Clipboard) start_listener(){
if event.xselectionrequest.selection == cb.selection {
mut xsre := &XSelectionRequestEvent{}
xsre = &event.xselectionrequest
mut xse := XSelectionEvent{
@type: C.SelectionNotify // 31
display: xsre.display
@ -355,22 +354,22 @@ fn (cb &Clipboard) pick_target(prop Property) Atom {
else
{
atom_list := &Atom(prop.data)
mut to_be_requested := Atom(0)
//This is higher than the maximum priority.
mut priority := math.max_i32
supported_targets := cb.get_supported_targets()
for i := 0; i < prop.nitems; i++ {
//See if this data type is allowed and of higher priority (closer to zero)
//than the present one.
if cb.is_supported_target(atom_list[i]) {
index := cb.get_target_index(atom_list[i])
if(priority > index && index >= 0)
{
{
priority = index
to_be_requested = atom_list[i]
}
@ -383,7 +382,7 @@ fn (cb &Clipboard) pick_target(prop Property) Atom {
fn (cb &Clipboard) get_atoms(types ...atom_type) []Atom {
mut atoms := []Atom
for typ in types {
atoms << cb.atoms[typ]
atoms << cb.atoms[typ]
}
return atoms
}

View File

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

View File

@ -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
}