Giter VIP home page Giter VIP logo

Comments (5)

thomas-mangin avatar thomas-mangin commented on August 22, 2024

@Casper64 I am not sure if you can investigate or not.

from v.

Casper64 avatar Casper64 commented on August 22, 2024

The read method should only be used if a io.reader instance is passed when creating a new StringReader.

That's confusing, it should be possible to use the read method seperately. 👍

from v.

Casper64 avatar Casper64 commented on August 22, 2024

Sorry, to come back at this:

In your example you pass in a buffer of lenght 100. If you call read the stringreader will attempt to read 100 bytes. The given source is only 4 bytes long, so then the code tries to read from the io.reader contained in it, but that is not set, there is no data and the method returns an error.

To me this makes sense, but the error message doesn't.

I think in this case the method should return the io.Eof error. What do you think @thomas-mangin ?

from v.

thomas-mangin avatar thomas-mangin commented on August 22, 2024

Hi @Casper64, and thank you for answering.

You are right: I messed up my minimum viable example; my buffer length should have been smaller than the string length.

In the case above, if only 14 bytes were read, the function should return 14 and the next call should return Eof. I believe this is the expected behaviour of the function in the C language and what people would expect.

As the above buffer is 100, then either another system call to the OS is required to read more than 14 bytes (for networking, the data may still be in transit) or only 14 bytes are available. The next call would then return Eof.

The problem with the reader not getting set is still there with a small buffer size as you can see below where the buffer length is 4.

module main

import io.string_reader

fn main() {
	s := 'line 1\nline 2\n'

	// works
	mut r := string_reader.StringReader.new(source: s)
	assert r.read_all(false)! == s

	// fails
	mut b := []u8{len: 4}
	mut sr := string_reader.StringReader.new(source: s)
	sr.read(mut b)!
}
❯ v run bug.v  
V panic: result not set (reader is not set)
v hash: df7828d
0   bug                                 0x0000000104a76838 panic_result_not_set + 124
1   bug                                 0x0000000104a8d650 main__main + 1000
2   bug                                 0x0000000104a8d95c main + 84
3   dyld                                0x000000018ebe60e0 start + 2360

from v.

Casper64 avatar Casper64 commented on August 22, 2024

That will be fixed. When the length of the buffer is smaller or equal to the length of the provided source the string reader will read from that source always, regardless of whether a reader is set or not.

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.