<commit_before>package de.bit3.jsass.context;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import de.bit3.jsass.importer.Importer;
import de.bit3.jsass.Options;
import de.bit3.jsass.OutputStyle;
import de.bit3.jsass.function.FunctionCallbackFactory;
import sass.SassLibrary;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

public class ContextFactory {
    private final SassLibrary SASS;

    public ContextFactory(SassLibrary SASS) {
        this.SASS = SASS;
    }

    public SassLibrary.Sass_File_Context create(FileContext context) {
        File    inputPath  = context.getInputPath();
        File    outputPath = context.getOutputPath();
        Options options    = context.getOptions();

        // create context
        SassLibrary.Sass_File_Context fileContext = SASS.sass_make_file_context(inputPath.getAbsolutePath());

        // configure context
        SassLibrary.Sass_Options libsassOptions = SASS.sass_file_context_get_options(fileContext);
        configure(inputPath, outputPath, libsassOptions, options);

        return fileContext;
    }

    public SassLibrary.Sass_Data_Context create(StringContext context) {
        String  string     = context.getString();
        Charset charset    = context.getCharset();
        File    inputPath  = context.getInputPath();
        File    outputPath = context.getOutputPath();
        Options options    = context.getOptions();

        byte[]  bytes  = string.getBytes(charset);
        Pointer memory = new Memory(bytes.length + 1);
        memory.write(0, bytes, 0, bytes.length);
        memory.setByte(bytes.length, (byte) 0);

        // create context
        SassLibrary.Sass_Data_Context dataContext = SASS.sass_make_data_context(memory);

        // configure context
        SassLibrary.Sass_Options libsassOptions = SASS.sass_data_context_get_options(dataContext);
        configure(inputPath, outputPath, libsassOptions, options);

        return dataContext;
    }

    /**
     * Configure sass.
     *
     * @param libsassOptions The libsass options.
     * @param javaOptions    The java options.
     */
    private void configure(File inputPath, File outputPath, SassLibrary.Sass_Options libsassOptions, Options javaOptions) {
        int    precision           = javaOptions.getPrecision();
        int    outputStyle         = mapOutputStyle(javaOptions.getOutputStyle());
        byte   sourceComments      = createBooleanByte(javaOptions.isSourceComments());
        byte   sourceMapEmbed      = createBooleanByte(javaOptions.isSourceMapEmbed());
        byte   sourceMapContents   = createBooleanByte(javaOptions.isSourceMapContents());
        byte   omitSourceMapUrl    = createBooleanByte(javaOptions.isOmitSourceMapUrl());
        byte   isIndentedSyntaxSrc = createBooleanByte(javaOptions.isIndentedSyntaxSrc());
        String inputPathString     = null == inputPath ? "" : inputPath.getAbsolutePath();
        String outputPathString    = null == outputPath ? "" : outputPath.getAbsolutePath();
        String imagePath           = javaOptions.getImageUrl();
        String includePaths        = joinFilePaths(javaOptions.getIncludePaths());
        String sourceMapFile = null == javaOptions.getSourceMapFile()
                ? ""
                : javaOptions.getSourceMapFile().getAbsolutePath();<commit_msg>Derive a byte buffer from the memory object, when compiling strings.<commit_after>package de.bit3.jsass.context;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import de.bit3.jsass.importer.Importer;
import de.bit3.jsass.Options;
import de.bit3.jsass.OutputStyle;
import de.bit3.jsass.function.FunctionCallbackFactory;
import sass.SassLibrary;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;

public class ContextFactory {
    private final SassLibrary SASS;

    public ContextFactory(SassLibrary SASS) {
        this.SASS = SASS;
    }

    public SassLibrary.Sass_File_Context create(FileContext context) {
        File    inputPath  = context.getInputPath();
        File    outputPath = context.getOutputPath();
        Options options    = context.getOptions();

        // create context
        SassLibrary.Sass_File_Context fileContext = SASS.sass_make_file_context(inputPath.getAbsolutePath());

        // configure context
        SassLibrary.Sass_Options libsassOptions = SASS.sass_file_context_get_options(fileContext);
        configure(inputPath, outputPath, libsassOptions, options);

        return fileContext;
    }

    public SassLibrary.Sass_Data_Context create(StringContext context) {
        String  string     = context.getString();
        Charset charset    = context.getCharset();
        File    inputPath  = context.getInputPath();
        File    outputPath = context.getOutputPath();
        Options options    = context.getOptions();

        byte[]  bytes  = string.getBytes(charset);
        Memory memory = new Memory(bytes.length + 1);
        memory.write(0, bytes, 0, bytes.length);
        memory.setByte(bytes.length, (byte) 0);

        ByteBuffer buffer = memory.getByteBuffer(0, memory.size());

        // create context
        SassLibrary.Sass_Data_Context dataContext = SASS.sass_make_data_context(buffer);

        // configure context
        SassLibrary.Sass_Options libsassOptions = SASS.sass_data_context_get_options(dataContext);
        configure(inputPath, outputPath, libsassOptions, options);

        return dataContext;
    }

    /**
     * Configure sass.
     *
     * @param libsassOptions The libsass options.
     * @param javaOptions    The java options.
     */
    private void configure(File inputPath, File outputPath, SassLibrary.Sass_Options libsassOptions, Options javaOptions) {
        int    precision           = javaOptions.getPrecision();
        int    outputStyle         = mapOutputStyle(javaOptions.getOutputStyle());
        byte   sourceComments      = createBooleanByte(javaOptions.isSourceComments());
        byte   sourceMapEmbed      = createBooleanByte(javaOptions.isSourceMapEmbed());
        byte   sourceMapContents   = createBooleanByte(javaOptions.isSourceMapContents());
        byte   omitSourceMapUrl    = createBooleanByte(javaOptions.isOmitSourceMapUrl());
        byte   isIndentedSyntaxSrc = createBooleanByte(javaOptions.isIndentedSyntaxSrc());
        String inputPathString     = null == inputPath ? "" : inputPath.getAbsolutePath();
        String outputPathString    = null == outputPath ? "" : outputPath.getAbsolutePath();
        String imagePath           = javaOptions.getImageUrl();
        String includePaths        = joinFilePaths(javaOptions.getIncludePaths());
        String sourceMapFile = null == javaOptions.getSourceMapFile()
                ? ""
                : javaOptions.getSourceMapFile().getAbsolutePath();
{'fraction_non_alphanumeric': 0.0897305171158048, 'fraction_numerical': 0.002767662053896577, 'mean_word_length': 3.0675355450236967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6268316', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1855, 'n_words': 369}