<commit_before>/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2009-2018 Brazil
  Copyright(C) 2018-2019 <NAME> <<EMAIL>>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "grn.h"

#include <string.h>

#include "grn_db.h"
#include "grn_expr.h"
#include "grn_output.h"
#include "grn_output_columns.h"
#include "grn_str.h"
#include "grn_util.h"

#define LEVELS (&ctx->impl->output.levels)
#define DEPTH (GRN_BULK_VSIZE(LEVELS)>>2)
#define CURR_LEVEL (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1))) : 0)
#define INCR_DEPTH(i) GRN_UINT32_PUT(ctx, LEVELS, i)
#define DECR_DEPTH (DEPTH ? grn_bulk_truncate(ctx, LEVELS, GRN_BULK_VSIZE(LEVELS) - sizeof(uint32_t)) : 0)
#define INCR_LENGTH (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1)) += 2) : 0)

grn_rc
grn_output_range_normalize(grn_ctx *ctx, int size, int *offset, int *limit)
{
  int end;
  int normalized_offset = *offset;
  int normalized_limit = *limit;

  if (normalized_limit < 0) {
    normalized_limit += size + 1;
    if (limit < 0) {
      *offset = 0;
      *limit = 0;
      return GRN_TOO_SMALL_LIMIT;
    }
  } else if (normalized_limit > size) {
    normalized_limit = size;
  }

  if (normalized_offset < 0) {
    normalized_offset += size;
    if (normalized_offset < 0) {
      if (normalized_limit + normalized_offset >= 0) {
        normalized_limit += normalized_offset;
        normalized_offset = 0;
      } else {
        *offset = 0;
        *limit = 0;
        return GRN_TOO_SMALL_OFFSET;
      }
    }
  } else if (normalized_offset != 0 && normalized_offset >= size) {
    *offset = 0;
    *limit = 0;
    return GRN_TOO_LARGE_OFFSET;
  }

  /* At this point, offset and limit must be zero or positive. */
  end = normalized_offset + normalized_limit;<commit_msg>grn_output_range_normalize: fix wrong variable reference (#1017)<commit_after>/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2009-2018 Brazil
  Copyright(C) 2018-2019 <NAME> <<EMAIL>>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "grn.h"

#include <string.h>

#include "grn_db.h"
#include "grn_expr.h"
#include "grn_output.h"
#include "grn_output_columns.h"
#include "grn_str.h"
#include "grn_util.h"

#define LEVELS (&ctx->impl->output.levels)
#define DEPTH (GRN_BULK_VSIZE(LEVELS)>>2)
#define CURR_LEVEL (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1))) : 0)
#define INCR_DEPTH(i) GRN_UINT32_PUT(ctx, LEVELS, i)
#define DECR_DEPTH (DEPTH ? grn_bulk_truncate(ctx, LEVELS, GRN_BULK_VSIZE(LEVELS) - sizeof(uint32_t)) : 0)
#define INCR_LENGTH (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1)) += 2) : 0)

grn_rc
grn_output_range_normalize(grn_ctx *ctx, int size, int *offset, int *limit)
{
  int end;
  int normalized_offset = *offset;
  int normalized_limit = *limit;

  if (normalized_limit < 0) {
    normalized_limit += size + 1;
    if (normalized_limit < 0) {
      *offset = 0;
      *limit = 0;
      return GRN_TOO_SMALL_LIMIT;
    }
  } else if (normalized_limit > size) {
    normalized_limit = size;
  }

  if (normalized_offset < 0) {
    normalized_offset += size;
    if (normalized_offset < 0) {
      if (normalized_limit + normalized_offset >= 0) {
        normalized_limit += normalized_offset;
        normalized_offset = 0;
      } else {
        *offset = 0;
        *limit = 0;
        return GRN_TOO_SMALL_OFFSET;
      }
    }
  } else if (normalized_offset != 0 && normalized_offset >= size) {
    *offset = 0;
    *limit = 0;
    return GRN_TOO_LARGE_OFFSET;
  }

  /* At this point, offset and limit must be zero or positive. */
  end = normalized_offset + normalized_limit;
{'fraction_non_alphanumeric': 0.10478170478170479, 'fraction_numerical': 0.025363825363825365, 'mean_word_length': 3.57319391634981, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '4348606', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1658, 'n_words': 508}