<commit_before>// Copyright <NAME>, SwampBits LLC 2014
// BSD License

#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "StrUtils.h"
#include "NumberFormatException.h"
#include "zlib.h"

static const std::string EMPTY = "";
static const std::string SPACE = " ";
static const std::string ZERO = "0";

using namespace chaudiere;

//******************************************************************************

bool OnlyIntegerDigits(const std::string& s) {
   // scan for valid characters (0-9, and possibly '-' in first position)
   for (int i = 0; i < s.length(); ++i) {
      char c = s[i];
      int digit_value = c - '0';
      if (digit_value < 0 || digit_value > 9) {
         if (0 == i && c == '-') {
            // a minus sign as first character is our only exception to 0-9
         } else {
            return false;
         }
      }
   }
   return true;
}

//******************************************************************************

int StrUtils::parseInt(const std::string& s) {
   if (!OnlyIntegerDigits(s)) {
      throw NumberFormatException(s);
   }

   int intValue = ::atoi(s.c_str());
   if (intValue == 0) {
      if (s != ZERO) {
         throw NumberFormatException(s);
      }
   }

   return intValue;
}

//******************************************************************************

long StrUtils::parseLong(const std::string& s) {
   if (!OnlyIntegerDigits(s)) {
      throw NumberFormatException(s);
   }

   long longValue = ::atol(s.c_str());
   if (longValue == 0) {
      if (s != ZERO) {
         throw NumberFormatException(s);
      }
   }

   return longValue;
}

//******************************************************************************

std::string StrUtils::toString(int i) {
   char buffer[40];
   ::memset(buffer, 0, sizeof(buffer));
   ::snprintf(buffer, sizeof(buffer), "%d", i);<commit_msg>expand OnlyIntegerDigits to accommodate decimal if needed<commit_after>// Copyright <NAME>, SwampBits LLC 2014
// BSD License

#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "StrUtils.h"
#include "NumberFormatException.h"
#include "zlib.h"

static const std::string EMPTY = "";
static const std::string SPACE = " ";
static const std::string ZERO = "0";

using namespace chaudiere;

//******************************************************************************

bool OnlyIntegerDigits(const std::string& s, bool allow_decimal=false) {
   // scan for valid characters (0-9)
   // '-' is allowed as first character only
   // if allow_decimal is true, '.' may only have single occurrence
   int decimals_found = 0;
   const int s_length = s.length();
   for (int i = 0; i < s_length; ++i) {
      const char c = s[i];
      if (c == '-') {
         if (i > 0) {
            return false;
         }
      } else if (allow_decimal && (c == '.')) {
         decimals_found++;
         if (decimals_found > 1) {
            return false;
         }
      } else {
         const int digit_value = c - '0';
         if (digit_value < 0 || digit_value > 9) {
            return false;
         }
      }
   }
   return true;
}

//******************************************************************************

int StrUtils::parseInt(const std::string& s) {
   if (!OnlyIntegerDigits(s)) {
      throw NumberFormatException(s);
   }

   const int intValue = ::atoi(s.c_str());
   if (intValue == 0) {
      if (s != ZERO) {
         throw NumberFormatException(s);
      }
   }

   return intValue;
}

//******************************************************************************

long StrUtils::parseLong(const std::string& s) {
   if (!OnlyIntegerDigits(s)) {
      throw NumberFormatException(s);
   }

   const long longValue = ::atol(s.c_str());
   if (longValue == 0) {
      if (s != ZERO) {
         throw NumberFormatException(s);
      }
   }

   return longValue;
}

//******************************************************************************

float StrUtils::parseFloat(const std::string& s) {
   if (!OnlyIntegerDigits(s, true)) {
      throw NumberFormatException(s);
   }

   const float floatValue = ::atof(s.c_str());
   if (floatValue == 0) {
      if (s != ZERO) {
         throw NumberFormatException(s);
      }
   }

   return floatValue;
}

//******************************************************************************

double StrUtils::parseDouble(const std::string& s) {
   if (!OnlyIntegerDigits(s, true)) {
      throw NumberFormatException(s);
   }

   const double doubleValue = ::atof(s.c_str());
   if (doubleValue == 0) {
      if (s != ZERO) {
         throw NumberFormatException(s);
      }
   }

   return doubleValue;
}

//******************************************************************************

std::string StrUtils::toString(int i) {
   char buffer[40];
   ::memset(buffer, 0, sizeof(buffer));
   ::snprintf(buffer, sizeof(buffer), "%d", i);
{'fraction_non_alphanumeric': 0.28462157809983896, 'fraction_numerical': 0.008051529790660225, 'mean_word_length': 3.2397610921501707, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5476415', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1431, 'n_words': 371}