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

View File

@ -25,25 +25,25 @@ const (
*/ */
pub fn decode(data string) string { pub fn decode(data string) string {
buffer := malloc( data.len * 3 / 4 ) 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. * decode - expects a string. Returns its base64 encoded version.
* @param data - the input string. * @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: 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. * NB: if you need to encode many strings repeatedly, take a look at encode_in_buffer too.
*/ */
pub fn encode(data string) string { pub fn encode(data string) string {
buffer := malloc( 4 * ((data.len + 2) / 3) ) 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. * 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 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. * 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. * @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. * 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. * 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 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. * 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. * @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. * 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 input_length := data.len
output_length := 4 * ((input_length + 2) / 3) output_length := 4 * ((input_length + 2) / 3)

View File

@ -3,26 +3,26 @@ import encoding.base64
fn test_long_encoding(){ fn test_long_encoding(){
repeats := 1000 repeats := 1000
input_size := 3000 input_size := 3000
s_original := 'a'.repeat(input_size) s_original := 'a'.repeat(input_size)
s_encoded := base64.encode(s_original) s_encoded := base64.encode(s_original)
s_decoded := base64.decode(s_encoded) s_decoded := base64.decode(s_encoded)
assert s_encoded.len > s_original.len assert s_encoded.len > s_original.len
assert s_original == s_decoded assert s_original == s_decoded
mut s := 0 mut s := 0
ebuffer := malloc( s_encoded.len ) ebuffer := malloc( s_encoded.len )
for i := 0; i < repeats; i++ { 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 s += resultsize
assert resultsize == s_encoded.len assert resultsize == s_encoded.len
} }
dbuffer := malloc( s_decoded.len ) dbuffer := malloc( s_decoded.len )
for i := 0; i < repeats; i++ { 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 s += resultsize
assert resultsize == s_decoded.len assert resultsize == s_decoded.len
} }