Giter Club home page Giter Club logo

Comments (1)

dhess avatar dhess commented on August 16, 2024

ringbuf_findchr returns ringbuf_bytes_used when there is no match, so it only returns 0 when the ring buffer is empty. (If the ring buffer is not empty and there is no match, then the value returned will be > 0.)

It is expected that you will compare the value returned by ringbuf_findchr to the value returned by ringbuf_bytes_used to determine whether a match has been found, e.g.:

https://github.com/dhess/echoev/blob/c74ff579483e3aab5e10b088ef0ba63445dac0bf/echoevc.c#L337

If you compare the two values with <, then there is no ambiguity.

The reason that ringbuf_findchr returns ringbuf_bytes_used when there is no match, rather than returning -1 or some other sentinel value, is so that the type of the returned value can be size_t rather than ssize_t; and the reason for that is for type safety, since ringbuf_findchr itself takes size_t offset values. (Negative offsets make no sense.)

So while this convention is slightly clumsier and a bit more expensive than checking for -1 (because this convention requires an additional call to ringbuf_bytes_used), I think it's worth the type safety.

However, in retrospect, this is all a bit confusing, and I should probably make a note of this convention in the documentation.

from c-ringbuf.

Related Issues (9)

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.