8000 Add read_until_delimiter macro by sudison · Pull Request #2259 · c3lang/c3c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add read_until_delimiter macro #2259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sudison
Copy link
Contributor
@sudison sudison commented Jul 1, 2025

This macro reads from an input stream byte by byte until a specified delimiter is found, writing each byte to an output stream. It returns the number of bytes read (excluding the delimiter) or an error.

Key features:

  • Reads byte-by-byte until delimiter is encountered
  • Writes data to output stream as it reads
  • Proper error handling for EOF and write failures
  • Compile-time type checking with @require constraints

Potential use cases:

  • Parse HTTP headers (read until '\r\n')
  • Extract fields from CSV files (read until ',')
  • Parse configuration files (read until '=')
  • Process line-based protocols
  • Extract tokens from streams

Added comprehensive tests covering:

  • Basic functionality with delimiter found
  • EOF handling when delimiter not found
  • Empty input streams
  • Immediate delimiter at start
  • Output stream write failures

@sudison sudison force-pushed the read-util-delimiter branch from 7fd7248 to 74d16a6 Compare July 1, 2025 09:18
This macro reads from an input stream byte by byte until a specified
delimiter is found, writing each byte to an output stream. It returns
the number of bytes read (excluding the delimiter) or an error.

Key features:
- Unified function handles both single character and multi-character delimiters
- Reads byte-by-byte until delimiter is encountered
- Writes data to output stream as it reads
- Proper error handling for EOF and write failures
- Compile-time type checking with @require constraints
- Optimized pattern matching for multi-character delimiters

Potential use cases:
- Parse HTTP headers (read until '\r\n')
- Extract fields from CSV files (read until ',')
- Parse configuration files (read until '=')
- Process line-based protocols
- Extract tokens from streams

Added comprehensive tests covering:
- Basic functionality with single and multi-character delimiters
- EOF handling when delimiter not found
- Empty input streams and empty delimiters
- Immediate delimiter at start
- Partial matches that don't complete
- Output stream write failures
@sudison sudison force-pushed the read-util-delimiter branch from 74d16a6 to 76deeaf Compare July 1, 2025 09:22
@BWindey
Copy link
Contributor
BWindey commented Jul 1, 2025

It might be worth adding delimeter = '\n' to readline in io/io.c3 instead of a whole new function.

@lerno
Copy link
Collaborator
lerno commented Jul 2, 2025

Delimiter should be char[]. Temp allocation here is a bit unfortunate, and wrapping it in a @pool is not ideal. Consider @stack_mem for it.

@lerno lerno added the Discussion needed This feature needs discussion to iron out details label Jul 2, 2025
@lerno
Copy link
Collaborator
lerno commented Jul 5, 2025

This will not work for the following example:

Data:

apapapbb

Delimiter:

apapbb

The code will not find the delimiter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion needed This feature needs discussion to iron out details
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0