<commit_before>#include "font_loader.hpp"
#include "cpp/ylikuutio/file/file_loader.hpp"
#include "cpp/ylikuutio/string/ylikuutio_string.hpp"

// Include standard headers
#include <cstdio>   // std::FILE, std::fclose, std::fopen, std::fread, std::getchar, std::printf etc.
#include <cstring>  // std::memcmp, std::strcmp, std::strlen, std::strncmp
#include <iostream> // std::cout, std::cin, std::cerr
#include <string>   // std::string
#include <vector>   // std::vector

namespace model
{
    bool check_if_we_are_inside_block(const char* SVG_base_pointer, char*& SVG_data_pointer)
    {
        // This function returns `true` if we are inside block, `false` otherwise.

        std::vector<std::string> identifier_strings_vector;

        // All possible block identifier strings.
        identifier_strings_vector = { "<?xml ", "<!DOCTYPE ", "<svg>", "<metadata>", "</metadata>", "<defs>", "<font ", "<font-face", "<missing-glyph" };
        return string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, identifier_strings_vector);
    }

    int32_t extract_value_from_string_with_standard_endings(char*& vertex_data_pointer, const char* description)
    {
        return string::extract_value_from_string(vertex_data_pointer, (char*) " Mmhvz\">", description);
    }

    bool find_first_glyph_in_SVG(const char* SVG_base_pointer, char*& SVG_data_pointer)
    {
        // This function advances `SVG_data_pointer` to the start of the first glyph.
        // Returns true if a glyph was found.
        // Returns false if no glyph was found.

        bool is_inside_block = false;

        // Read bytes of SVG data until `"<glyph "` is encountered!
        while (true)
        {
            if (!is_inside_block)
            {
                // OK, were are not inside a block.
                if (SVG_data_pointer == nullptr)
                {
                    return false;
                }

                if (string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, std::vector<std::string> { "<glyph" }))
                {
                    return true;
                }
                if (string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, std::vector<std::string> { "</svg>" }))
                {
                    return false;
                }

                is_inside_block = model::check_if_we_are_inside_block(SVG_base_pointer, SVG_data_pointer);
                SVG_data_pointer++;
            }
            else
            {
                // OK, we are inside a block.
                SVG_data_pointer = std::strchr(SVG_data_pointer, '>');
                string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, std::vector<std::string> { ">" });
                is_inside_block = false;
            }
        }
    }

    void extract_string(
            char* dest_mem_pointer,
            char* &src_mem_pointer,
            char* char_end_string)
    {
        while (std::strncmp(src_mem_pointer, char_end_string, std::strlen(char_end_string)) != 0)
        {
            strncpy(dest_mem_pointer++, src_mem_pointer++, 1);
        }
        *dest_mem_pointer = '\0';
    }

    bool load_vertex_data(
            const char* SVG_base_pointer,
            char*& SVG_data_pointer,
            std::vector<std::vector<glm::vec3>> &current_glyph_vertices)
    {
        // This function returns a pointer to vertex data of a single glyph and advances `SVG_data_pointer`.

        std::vector<glm::vec3> vertices_of_current_edge_section;    // vertices of the current edge section.
        // d=" was found.
        // Follow the path and create the vertices accordingly.

        // Find the memory address of the opening double quote.
        char* opening_double_quote_pointer = strchr(SVG_data_pointer, '"');
        if (opening_double_quote_pointer == nullptr)
        {
            std::cerr << "error: no opening double quote found for d=!\n";
            return false;
        }

        // Find the memory address of the closing double quote.
        char* closing_double_quote_pointer = strchr(++opening_double_quote_pointer, '"');
        if (closing_double_quote_pointer == nullptr)
        {
            std::cerr << "error: no closing double quote found for d=!\n";
            return false;
        }

        glm::vec3 current_vertex;
        current_vertex.z = 0; // z is not defined in the path (originally these are not 3D fonts!).
        char char_path[1024];

        // copy from opening double quote to the next `"/"`.
        model::extract_string(char_path, opening_double_quote_pointer, (char*) "/");

        std::printf("d: %s\n", char_path);

        // Loop through vertices and push them to `current_glyph_vertices`.
        char* vertex_data_pointer;
        vertex_data_pointer = char_path;

        while (true)
        {
            if (std::strncmp(vertex_data_pointer, "M", std::strlen("M")) == 0)
            {
                current_vertex.x = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "M (moveto)");

                while (true)
                {
                    if (std::strncmp(vertex_data_pointer, " ", std::strlen(" ")) == 0)
                    {
                        current_vertex.y = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                                (const char*) "space (moveto y coordinate)");
                        vertices_of_current_edge_section.push_back(current_vertex);
                        break;
                    } // if (std::strncmp(vertex_data_pointer, " ", std::strlen(" ")) == 0)
                    vertex_data_pointer++;
                } // while (true)
            } // if (std::strncmp(vertex_data_pointer, "M", std::strlen("M")) == 0)
            else if (std::strncmp(vertex_data_pointer, "h", std::strlen("h")) == 0)
            {
                // OK, this is horizontal relative lineto.
                int32_t horizontal_lineto_value = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "h (horizontal relative lineto)");
                current_vertex.x += horizontal_lineto_value;
                vertices_of_current_edge_section.push_back(current_vertex);
            } // else if (std::strncmp(vertex_data_pointer, "h", std::strlen("h")) == 0)
            else if (std::strncmp(vertex_data_pointer, "v", std::strlen("v")) == 0)
            {
                // OK, this is vertical relative lineto.
                int32_t vertical_lineto_value = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "v (vertical relative lineto)");
                current_vertex.y += vertical_lineto_value;
                vertices_of_current_edge_section.push_back(current_vertex);
            } // else if (std::strncmp(vertex_data_pointer, "v", std::strlen("v")) == 0)
            else if (std::strncmp(vertex_data_pointer, "z", std::strlen("z")) == 0)
            {
                std::printf("z (closepath)\n");
                current_glyph_vertices.push_back(vertices_of_current_edge_section); // store the vertices of the current edge section.
                vertices_of_current_edge_section.clear();                           // clear the vector of vertices of the current edge section.
                vertex_data_pointer++;
            } // else if (std::strncmp(vertex_data_pointer, "z", std::strlen("z")) == 0)
            else if (std::strncmp(vertex_data_pointer, "\"", std::strlen("\"")) == 0)<commit_msg>Refactoring: `while (true)` and `if` converted to a `while` loop.<commit_after>#include "font_loader.hpp"
#include "cpp/ylikuutio/file/file_loader.hpp"
#include "cpp/ylikuutio/string/ylikuutio_string.hpp"

// Include standard headers
#include <cstdio>   // std::FILE, std::fclose, std::fopen, std::fread, std::getchar, std::printf etc.
#include <cstring>  // std::memcmp, std::strcmp, std::strlen, std::strncmp
#include <iostream> // std::cout, std::cin, std::cerr
#include <string>   // std::string
#include <vector>   // std::vector

namespace model
{
    bool check_if_we_are_inside_block(const char* SVG_base_pointer, char*& SVG_data_pointer)
    {
        // This function returns `true` if we are inside block, `false` otherwise.

        std::vector<std::string> identifier_strings_vector;

        // All possible block identifier strings.
        identifier_strings_vector = { "<?xml ", "<!DOCTYPE ", "<svg>", "<metadata>", "</metadata>", "<defs>", "<font ", "<font-face", "<missing-glyph" };
        return string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, identifier_strings_vector);
    }

    int32_t extract_value_from_string_with_standard_endings(char*& vertex_data_pointer, const char* description)
    {
        return string::extract_value_from_string(vertex_data_pointer, (char*) " Mmhvz\">", description);
    }

    bool find_first_glyph_in_SVG(const char* SVG_base_pointer, char*& SVG_data_pointer)
    {
        // This function advances `SVG_data_pointer` to the start of the first glyph.
        // Returns true if a glyph was found.
        // Returns false if no glyph was found.

        bool is_inside_block = false;

        // Read bytes of SVG data until `"<glyph "` is encountered!
        while (true)
        {
            if (!is_inside_block)
            {
                // OK, were are not inside a block.
                if (SVG_data_pointer == nullptr)
                {
                    return false;
                }

                if (string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, std::vector<std::string> { "<glyph" }))
                {
                    return true;
                }
                if (string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, std::vector<std::string> { "</svg>" }))
                {
                    return false;
                }

                is_inside_block = model::check_if_we_are_inside_block(SVG_base_pointer, SVG_data_pointer);
                SVG_data_pointer++;
            }
            else
            {
                // OK, we are inside a block.
                SVG_data_pointer = std::strchr(SVG_data_pointer, '>');
                string::check_and_report_if_some_string_matches(SVG_base_pointer, SVG_data_pointer, std::vector<std::string> { ">" });
                is_inside_block = false;
            }
        }
    }

    void extract_string(
            char* dest_mem_pointer,
            char* &src_mem_pointer,
            char* char_end_string)
    {
        while (std::strncmp(src_mem_pointer, char_end_string, std::strlen(char_end_string)) != 0)
        {
            strncpy(dest_mem_pointer++, src_mem_pointer++, 1);
        }
        *dest_mem_pointer = '\0';
    }

    bool load_vertex_data(
            const char* SVG_base_pointer,
            char*& SVG_data_pointer,
            std::vector<std::vector<glm::vec3>> &current_glyph_vertices)
    {
        // This function returns a pointer to vertex data of a single glyph and advances `SVG_data_pointer`.

        std::vector<glm::vec3> vertices_of_current_edge_section;    // vertices of the current edge section.
        // d=" was found.
        // Follow the path and create the vertices accordingly.

        // Find the memory address of the opening double quote.
        char* opening_double_quote_pointer = strchr(SVG_data_pointer, '"');
        if (opening_double_quote_pointer == nullptr)
        {
            std::cerr << "error: no opening double quote found for d=!\n";
            return false;
        }

        // Find the memory address of the closing double quote.
        char* closing_double_quote_pointer = strchr(++opening_double_quote_pointer, '"');
        if (closing_double_quote_pointer == nullptr)
        {
            std::cerr << "error: no closing double quote found for d=!\n";
            return false;
        }

        glm::vec3 current_vertex;
        current_vertex.z = 0; // z is not defined in the path (originally these are not 3D fonts!).
        char char_path[1024];

        // copy from opening double quote to the next `"/"`.
        model::extract_string(char_path, opening_double_quote_pointer, (char*) "/");

        std::printf("d: %s\n", char_path);

        // Loop through vertices and push them to `current_glyph_vertices`.
        char* vertex_data_pointer;
        vertex_data_pointer = char_path;

        while (true)
        {
            if (std::strncmp(vertex_data_pointer, "M", std::strlen("M")) == 0)
            {
                current_vertex.x = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "M (moveto)");

                while (std::strncmp(vertex_data_pointer++, " ", std::strlen(" ")) != 0);

                current_vertex.y = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "space (moveto y coordinate)");
                vertices_of_current_edge_section.push_back(current_vertex);

            } // if (std::strncmp(vertex_data_pointer, "M", std::strlen("M")) == 0)
            else if (std::strncmp(vertex_data_pointer, "h", std::strlen("h")) == 0)
            {
                // OK, this is horizontal relative lineto.
                int32_t horizontal_lineto_value = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "h (horizontal relative lineto)");
                current_vertex.x += horizontal_lineto_value;
                vertices_of_current_edge_section.push_back(current_vertex);
            } // else if (std::strncmp(vertex_data_pointer, "h", std::strlen("h")) == 0)
            else if (std::strncmp(vertex_data_pointer, "v", std::strlen("v")) == 0)
            {
                // OK, this is vertical relative lineto.
                int32_t vertical_lineto_value = model::extract_value_from_string_with_standard_endings(vertex_data_pointer,
                        (const char*) "v (vertical relative lineto)");
                current_vertex.y += vertical_lineto_value;
                vertices_of_current_edge_section.push_back(current_vertex);
            } // else if (std::strncmp(vertex_data_pointer, "v", std::strlen("v")) == 0)
            else if (std::strncmp(vertex_data_pointer, "z", std::strlen("z")) == 0)
            {
                std::printf("z (closepath)\n");
                current_glyph_vertices.push_back(vertices_of_current_edge_section); // store the vertices of the current edge section.
                vertices_of_current_edge_section.clear();                           // clear the vector of vertices of the current edge section.
                vertex_data_pointer++;
            } // else if (std::strncmp(vertex_data_pointer, "z", std::strlen("z")) == 0)
            else if (std::strncmp(vertex_data_pointer, "\"", std::strlen("\"")) == 0)
{'fraction_non_alphanumeric': 0.1154632377184306, 'fraction_numerical': 0.003758654797230465, 'mean_word_length': 2.3149726775956285, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2473280', 'n_tokens_mistral': 4550, 'n_tokens_neox': 4384, 'n_words': 950}