nodepp icon indicating copy to clipboard operation
nodepp copied to clipboard

Does nodepp handle UTF-8 encoded files correctly? Can I extract codepoints (not characters) from the line returned by stream::line? Thanks

Open EDBCREPO opened this issue 1 year ago • 1 comments

Does nodepp handle UTF-8 encoded files correctly? Can I extract codepoints (not characters) from the line returned by stream::line? Thanks asked by Maximus94728.

EDBCREPO avatar Jun 23 '25 02:06 EDBCREPO

Hello! Apologies for the delayed response.

You've asked two excellent questions regarding UTF-8 handling and codepoint extraction in Nodepp. Let's address them:

Does Nodepp handle UTF-8 encoded files correctly?

Yes, Nodepp handles UTF-8 encoded files correctly at the byte level. When you read data from a file using Nodepp's file system utilities, the data is treated as raw bytes. This means Nodepp doesn't impose any specific character encoding interpretation by default. It's up to your application logic to interpret these bytes as UTF-8 characters if that's the expected encoding of your file.

Can I extract codepoints (not characters) from the line returned by stream::line?

While stream::line is designed for line-by-line reading based on newline characters, it's not the ideal tool for extracting arbitrary codepoints, especially those that might not be printable or part of a standard line structure.

For extracting specific codepoints, Nodepp provides more suitable methods, primarily through the file_t object's .read_until() function and the stream::until() utility. These functions allow you to read data from a stream until a specified byte (which can represent a codepoint's byte value) is encountered.

Let's illustrate with examples:

Method 1: Using file_t::read_until() for Pseudo-Asynchronous Control

This method provides a straightforward way to extract content until a specific byte is found.

Imagine you want to extract all content leading up to the 0x03 codepoint (ETX - End of Text), which is a non-printable control character.

#include <nodepp/nodepp.h>
#include <nodepp/fs.h>

using namespace nodepp;

void onMain() {
    auto file = fs::readable("FILE.txt");

    // Loop while the file has content available to read
    while (file.is_available()) {
        // Read data until the byte 0x03 is encountered
        // The read_until function returns the data up to (but not including) the delimiter.
        console::log("<>", file.read_until(
            type::cast<char>(0x03) // Cast the codepoint value to char
        ));

        // Although read_until itself might call process::next(), explicitly
        // calling it here reinforces the non-blocking nature and ensures
        // other pending tasks in the event loop get a chance to run,
        // especially if `read_until` quickly finds its delimiter.
        process::next();
    }
}

This example demonstrates a pseudo-asynchronous program that reads and logs segments of a file delimited by the 0x03 codepoint.

Method 2: Using stream::until with an Event-Driven Approach.

For a more reactive, event-driven approach, you can leverage stream::until. This function sets up a listener that will emit data whenever the specified delimiter is found in the stream.

#include <nodepp/nodepp.h>
#include <nodepp/fs.h>

using namespace nodepp;

void onMain() {
    auto file = fs::readable("FILE.txt");

    // Register a callback to be executed whenever data is available
    file.onData([=](string_t data) {
        console::log("<>", data);
    });

    // Start streaming data, delimited by the 0x03 codepoint
    stream::until(file, type::cast<char>(0x03));
}

In this example, the onData handler will be triggered each time stream::until successfully reads a segment of data terminated by 0x03.

Method 3: Advanced Control with Raw Generators for 100% Asynchronous Operation

For scenarios requiring fine-grained, 100% asynchronous control over the reading process, Nodepp's raw generators offer the most flexibility. This approach is beneficial when you need to interleave file reading with other asynchronous operations or manage complex flow control.

#include <nodepp/nodepp.h>
#include <nodepp/fs.h>

using namespace nodepp;

void onMain() {
    auto file = fs::readable("FILE.txt");
    // Initialize a generator function for `until` operation
    auto read = type::bind(_file_::until()); 

    process::add([=]() {
        coStart // Macro to start a coroutine block

        while (file.is_available()) {
            // Asynchronously wait for the `until` operation to complete.
            // The operation returns 1 on success, allowing us to proceed.
            coWait((*read)(&file, type::cast<char>(0x03)) == 1); 

            // Check the state of the generator; if it's 0 or less, it means the operation finished or failed.
            if (read->state <= 0) { 
                coEnd; // Exit the coroutine
            }
            
            // Log the data extracted by the `until` operation
            console::log("<>", read->data); 

            coNext; // Yield control back to the event loop and prepare for the next iteration
        }

        coStop // Macro to stop a coroutine block
    });
}

This example showcases how to use Nodepp's coroutine-like features to build fully asynchronous file processing logic, giving you complete control over when and how data segments (delimited by codepoints) are extracted and processed.

In summary, while stream::line is for literal lines, Nodepp provides robust mechanisms through read_until and stream::until (and raw generators for advanced cases) to handle custom delimiters, including specific codepoints, enabling you to work effectively with various file structures and encodings like UTF-8 at the byte level.

EDBCREPO avatar Jun 23 '25 02:06 EDBCREPO