Repository: shader-slang/slang
File: tools/slang-embed/slang-embed.cpp (C++)

Code before:
                    }
                    else
                    {
                        // Otherwise, we emit the byte as an octal
                        // escape sequence, being sure to emit a
                        // full three digits to avoid errorneous
                        // encoding if the following byte might
                        // represent a digit.
                        //
                        fprintf(outputFile, "\\%03o", c);
                    }
                    break;
                }
            }
            fprintf(outputFile, "\\n\"\n");
        }
    }

    static Slang::UnownedStringSlice getIncludedFileName(Slang::UnownedStringSlice line)
    {
        auto trimmedLine = line.trimStart();
        if (!trimmedLine.startsWith("#"))
        {
            return Slang::UnownedStringSlice();
        }
        auto preprocessorLine = trimmedLine.tail(sizeof "#" - 1).trimStart();
        if (!preprocessorLine.startsWith("include"))
        {
            return Slang::UnownedStringSlice();
        }
        auto includeLine = preprocessorLine.tail(sizeof "include" - 1).trimStart();

        return Slang::StringUtil::getAtInSplit(includeLine, ' ', 0);
    }

    void processInputFile()
    {
        // Note: Eventually we might support multiple input files in a
        // single invocation of the tool, but for now we only have
        // a single file to process.

        // We derive an output path simply by appending `.cpp` to the input
        // path, if not otherwise specified
        char* defaultOutputPath = (char*)malloc(strlen(inputPath) + strlen(".cpp") + 1);
        ScopedMemory outputPathCleanup(defaultOutputPath);
        strcpy(defaultOutputPath, inputPath);
        strcat(defaultOutputPath, ".cpp");
        if (!outputPath)
            outputPath = defaultOutputPath;

        FILE* outputFile = fopen(outputPath, "w");

Review comment (suggestion):
The Slang codebase usually uses `sizeof` with parens, e.g. `sizeof("#")`. At least I've never had to think about the precedence of `sizeof` before, not sure if this is confusing to anyone else :smile:

Code after:
                    }
                    else
                    {
                        // Otherwise, we emit the byte as an octal
                        // escape sequence, being sure to emit a
                        // full three digits to avoid errorneous
                        // encoding if the following byte might
                        // represent a digit.
                        //
                        fprintf(outputFile, "\\%03o", c);
                    }
                    break;
                }
            }
            fprintf(outputFile, "\\n\"\n");
        }
    }

    static Slang::UnownedStringSlice getIncludedFileName(Slang::UnownedStringSlice line)
    {
        auto trimmedLine = line.trimStart();
        if (!trimmedLine.startsWith("#"))
        {
            return Slang::UnownedStringSlice();
        }
        auto preprocessorLine = trimmedLine.tail(sizeof("#") - 1).trimStart();
        if (!preprocessorLine.startsWith("include"))
        {
            return Slang::UnownedStringSlice();
        }
        auto includeLine = preprocessorLine.tail(sizeof("include") - 1).trimStart();

        return Slang::StringUtil::getAtInSplit(includeLine, ' ', 0);
    }

    void processInputFile()
    {
        // Note: Eventually we might support multiple input files in a
        // single invocation of the tool, but for now we only have
        // a single file to process.

        // We derive an output path simply by appending `.cpp` to the input
        // path, if not otherwise specified
        char* defaultOutputPath = (char*)malloc(strlen(inputPath) + strlen(".cpp") + 1);
        ScopedMemory outputPathCleanup(defaultOutputPath);
        strcpy(defaultOutputPath, inputPath);
        strcat(defaultOutputPath, ".cpp");
        if (!outputPath)
            outputPath = defaultOutputPath;

        FILE* outputFile = fopen(outputPath, "w");