<commit_before>// Copyright (c) 2016 IBM Corporation.

#include "string-extern.h"

#include "std/core.h"
#include "std/listdef.h"
#include "strutils.h"
#include <regex>

using namespace tosca;

StringTerm& AfterFirst(Context& ctx, StringTerm& string, StringTerm& sep)
{
    const tosca::string& ustring = string.Unbox();
    const tosca::string& usep = sep.Unbox();
    tosca::string::size_type idx = ustring.find(usep);

    StringTerm& result = newStringTerm(ctx, (idx == tosca::string::npos) ? "" : ustring.substr(idx + 1).c_str());
    string.Release();
    sep.Release();
    return result;
}

StringTerm& BeforeFirst(Context& ctx, StringTerm& string, StringTerm& sep)
{
    const tosca::string& ustring = string.Unbox();
    const tosca::string& usep = sep.Unbox();
    tosca::string::size_type idx = ustring.find(usep);

    StringTerm& result = newStringTerm(ctx, (idx == tosca::string::npos) ? ustring : ustring.substr(0, idx));
    string.Release();
    sep.Release();
    return result;
}

Bool& StringEqual(Context& ctx, StringTerm& str1, StringTerm& str2)
{
    const tosca::string& ustr1 = str1.Unbox();
    const tosca::string& ustr2 = str2.Unbox();
    Bool& result = ustr1 == ustr2 ? newTRUE(ctx) : newFALSE(ctx);
    str1.Release();
    str2.Release();<commit_msg>Fix for issue #43.  basic_string::substr constructs an allocator using the default constructor.  That allocator contains no tosca::context, so it allocates memory on the heap that never ends up being freed.<commit_after>// Copyright (c) 2016 IBM Corporation.

#include "string-extern.h"

#include "std/core.h"
#include "std/listdef.h"
#include "strutils.h"
#include <memory.h>
#include <regex>

using namespace tosca;

StringTerm& AfterFirst(Context& ctx, StringTerm& string, StringTerm& sep)
{
    const tosca::string& ustring = string.Unbox();
    const tosca::string& usep = sep.Unbox();
    tosca::string::size_type idx = ustring.find(usep);

    StringTerm& result = newStringTerm(ctx, (idx == tosca::string::npos) ? "" : ustring.substr(idx + 1).c_str());
    string.Release();
    sep.Release();
    return result;
}

StringTerm& BeforeFirst(Context& ctx, StringTerm& string, StringTerm& sep)
{
    const tosca::string& ustring = string.Unbox();
    const tosca::string& usep = sep.Unbox();
    tosca::string::size_type idx = ustring.find(usep);

    sep.Release();

    if (idx == tosca::string::npos)
    {
        return string;
    }
    else
    {
        char *c_ubefore = (char*) malloc(idx+1);
        memcpy(c_ubefore, ustring.c_str(), idx);
        c_ubefore[idx] = '\0';
        StringTerm& result = newStringTerm(ctx, (const char *)c_ubefore);
        free(c_ubefore);
        string.Release();

        return result;
    }
}

Bool& StringEqual(Context& ctx, StringTerm& str1, StringTerm& str2)
{
    const tosca::string& ustr1 = str1.Unbox();
    const tosca::string& ustr2 = str2.Unbox();
    Bool& result = ustr1 == ustr2 ? newTRUE(ctx) : newFALSE(ctx);
    str1.Release();
    str2.Release();
{'fraction_non_alphanumeric': 0.15281948615281948, 'fraction_numerical': 0.011678345011678345, 'mean_word_length': 3.90671031096563, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '3618872', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1040, 'n_words': 246}