<commit_before>#include "oniguruma.h"
#include "onig-result.h"
#include "unicode-utils.h"

OnigResult::OnigResult(OnigRegion* region, const string& searchString) : searchString_(searchString) {
  region_ = region;
}

OnigResult::~OnigResult() {
  onig_region_free(region_, 1);
}

int OnigResult::Count() {
  return region_->num_regs;
}

int OnigResult::LocationAt(int index) {
  int bytes = *(region_->beg + index);
  return UnicodeUtils::characters_in_bytes(searchString_.data(), bytes);
}

int OnigResult::LengthAt(int index) {
  int bytes = *(region_->end + index) - *(region_->beg + index);
  const char *search = searchString_.data() + *(region_->beg + index);
  return UnicodeUtils::characters_in_bytes(search, bytes);
}
<commit_msg>Only convert to characters when bytes > 0<commit_after>#include "oniguruma.h"
#include "onig-result.h"
#include "unicode-utils.h"

OnigResult::OnigResult(OnigRegion* region, const string& searchString) : searchString_(searchString) {
  region_ = region;
}

OnigResult::~OnigResult() {
  onig_region_free(region_, 1);
}

int OnigResult::Count() {
  return region_->num_regs;
}

int OnigResult::LocationAt(int index) {
  int bytes = *(region_->beg + index);
  if (bytes > 0)
    return UnicodeUtils::characters_in_bytes(searchString_.data(), bytes);
  else
    return 0;
}

int OnigResult::LengthAt(int index) {
  int bytes = *(region_->end + index) - *(region_->beg + index);
  if (bytes > 0) {
    const char *search = searchString_.data() + *(region_->beg + index);
    return UnicodeUtils::characters_in_bytes(search, bytes);
  } else
    return 0;
}

{'fraction_non_alphanumeric': 0.15012562814070352, 'fraction_numerical': 0.0043969849246231155, 'mean_word_length': 5.271653543307087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '1412825', 'n_tokens_mistral': 555, 'n_tokens_neox': 537, 'n_words': 116}