<commit_before>/* bzflag
* Copyright (c) 1993 - 2008 <NAME>
*
* This package is free software;  you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <stdio.h>
#include <stdarg.h>
#include <sstream>
#include "plugin_utils.h"

const char* bzu_GetTeamName( bz_eTeamType team )
{
  switch (team){

  case eRedTeam:
    return "Red";

  case eGreenTeam:
    return "Green";

  case eBlueTeam:
    return "Blue";

  case ePurpleTeam:
    return "Purple";

  case eRogueTeam:
    return "Rogue";

  case eObservers:
    return "Observer";

  case eRabbitTeam:
    return "Rabbit";

  case eHunterTeam:
    return "Hunter";

  default:
    break;
  }

  return "Unknown";
}

std::string printTime ( bz_Time *ts, const char* timezone )
{
  std::string time;
  appendTime(time,ts,timezone);
  return time;
}

//Date: Mon, 23 Jun 2008 17:50:22 GMT

void appendTime ( std::string & text, bz_Time *ts, const char* timezone )
{
  switch(ts->dayofweek)
  {
    case 1:
      text += "Mon";
      break;
    case 2:
      text += "Tue";
      break;
    case 3:
      text += "Wed";
      break;
    case 4:
      text += "Thu";
      break;
    case 5:
      text += "Fri";
      break;
    case 6:
      text += "Sat";
      break;
    case 0:
      text += "Sun";
      break;
  }

  text += format(", %d ",ts->day);
  
  switch(ts->month)
  {
  case 0:
    text += "Jan";
    break;
  case 1:
    text += "Feb";
    break;
  case 2:
    text += "Mar";
    break;
  case 3:
    text += "Apr";
    break;
  case 4:
    text += "May";
    break;
  case 5:
    text += "Jun";
    break;
  case 6:
    text += "Jul";
    break;
  case 7:
    text += "Aug";
    break;
  case 8:
    text += "Sep";
    break;
  case 9:
    text += "Oct";
    break;
  case 10:
    text += "Nov";
    break;
  case 11:
    text += "Dec";
    break;
 }

  text += format(" %d %d:%d:%d ",ts->year,ts->hour,ts->minute,ts->second);
  if (timezone)
    text += timezone;
  else
    text += "GMT";
}

std::string no_whitespace(const std::string &s)
{
  const int sourcesize = (int)s.size();

  int count = 0, i = 0, j = 0;
  for (i = 0; i < sourcesize; i++)
    if (!isWhitespace(s[i]))
      count++;

  // create result string of correct size
  std::string result(count, ' ');

  for (i = 0, j = 0; i < sourcesize; i++)
    if (!isWhitespace(s[i]))
      result[j++] = s[i];

  return result;
}

std::string tolower(const std::string& s)
{
  std::string trans = s;

  for (std::string::iterator i=trans.begin(), end=trans.end(); i!=end; ++i)
    *i = ::tolower(*i);
  return trans;
}

std::string tolower(const char* s)
{
  std::string trans = s;

  for (std::string::iterator i=trans.begin(), end=trans.end(); i!=end; ++i)
    *i = ::tolower(*i);
  return trans;
}

std::string format(const char* fmt, ...)
{
  va_list args;
  va_start(args, fmt);
  char	temp[2048];
  vsprintf(temp,fmt, args);
  std::string result = temp;
  va_end(args);
  return result;
}

std::vector<std::string> tokenize(const std::string& in, const std::string &delims, const int maxTokens, const bool useQuotes, size_t offset)
{
  std::vector<std::string> tokens;
  int numTokens = 0;
  bool inQuote = false;

  std::ostringstream currentToken;

  std::string::size_type pos = in.find_first_not_of(delims,offset);
  int currentChar  = (pos == std::string::npos) ? -1 : in[pos];
  bool enoughTokens = (maxTokens && (numTokens >= (maxTokens-1)));

  while (pos != std::string::npos && !enoughTokens) {

    // get next token
    bool tokenDone = false;
    bool foundSlash = false;

    currentChar = (pos < in.size()) ? in[pos] : -1;
    while ((currentChar != -1) && !tokenDone){

      tokenDone = false;

      if (delims.find(currentChar) != std::string::npos && !inQuote) { // currentChar is a delim
	pos ++;
	break; // breaks out of while loop
      }

      if (!useQuotes){
	currentToken << char(currentChar);
      } else {

	switch (currentChar){
	  case '\\' : // found a backslash
	    if (foundSlash){
	      currentToken << char(currentChar);
	      foundSlash = false;
	    } else {
	      foundSlash = true;
	    }
	    break;
	  case '\"' : // found a quote
	    if (foundSlash){ // found \"
	      currentToken << char(currentChar);
	      foundSlash = false;
	    } else { // found unescaped "
	      if (inQuote){ // exiting a quote
		// finish off current token
		tokenDone = true;
		inQuote = false;
		//slurp off one additional delimeter if possible
		if (pos+1 < in.size() &&
		  delims.find(in[pos+1]) != std::string::npos) {
		    pos++;
		  }

	      } else { // entering a quote
		// finish off current token
		tokenDone = true;
		inQuote = true;
	      }
	    }
	    break;
	  default:
	    if (foundSlash){ // don't care about slashes except for above cases
	      currentToken << '\\';
	      foundSlash = false;
	    }
	    currentToken << char(currentChar);
	    break;
	}
      }

      pos++;
      currentChar = (pos < in.size()) ? in[pos] : -1;
    } // end of getting a Token

    if (currentToken.str().size() > 0) { // if the token is something add to list
      tokens.push_back(currentToken.str());
      currentToken.str("");
      numTokens ++;
    }

    enoughTokens = (maxTokens && (numTokens >= (maxTokens-1)));
    if (enoughTokens)
      break;
    else
      pos = in.find_first_not_of(delims,pos);

  } // end of getting all tokens -- either EOL or max tokens reached

  if (enoughTokens && pos != std::string::npos) {
    std::string lastToken = in.substr(pos);
    if (lastToken.size() > 0)
      tokens.push_back(lastToken);
  }

  return tokens;
}

std::string replace_all(const std::string& in, const std::string& replaceMe, const std::string& withMe)
{
  std::string result;
  std::string::size_type beginPos = 0;
  std::string::size_type endPos = 0;
  std::ostringstream tempStream;

  endPos = in.find(replaceMe);
  if (endPos == std::string::npos)
    return in; // can't find anything to replace
  if (replaceMe.empty()) return in; // can't replace nothing with something -- can do reverse

  while (endPos != std::string::npos) {
    // push the  part up to
    tempStream << in.substr(beginPos,endPos-beginPos);
    tempStream << withMe;
    beginPos = endPos + replaceMe.size();
    endPos = in.find(replaceMe,beginPos);
  }
  tempStream << in.substr(beginPos);
  return tempStream.str();
}

std::string url_encode(const std::string &text)
{
  char hex[5];
  std::string destination;
  for (int i=0;  i < (int) text.size(); i++) {
    char c = text[i];
    if (isAlphanumeric(c)) {
      destination+=c;
    } else if (isWhitespace(c)) {
      destination+='+';
    } else {
      destination+='%';
      sprintf(hex, "%-2.2X", c);
      destination.append(hex);
    }
  }
  return destination;
}

std::string url_decode(const std::string &text)
{
  std::string destination;

  std::string::const_iterator itr = text.begin();
  while ( itr != text.end() ) {
    if (*itr != '%') {
      destination += *itr++;
    } else {
      char hex[5] = "0x00";

      itr++;;
      if (itr == text.end())
	return destination;

      hex[2] = *itr;

      itr++;;
      if (itr == text.end())
	return destination;

      hex[3] = *itr;

      int val = 0;
      sscanf(hex,"%x",&val);
      if ( val != 0 )
	destination += (char)val;
      itr++;
    }
  }
  return destination;
}

size_t find_first_substr(const std::string &findin, const std::string findwhat, size_t offset)
{
  if (findwhat.size())
  {
    for ( size_t f = offset; f < findin.size(); f++ )
    {
      if (findin[f] == findwhat[0])
      {
	size_t start = f;
	for ( size_t w = 1; w < findwhat.size(); w++ )
	{
	  if ( f+w > findin.size())
	    return std::string::npos;
	  if (findin[f+w] != findwhat[w])
	  {
	    f+=w;
	    w = findwhat.size();
	  }
	}
	if (start == f)
	  return f;
      }
    }
  }
  return std::string::npos;
}

void trimLeadingWhitespace ( std::string &text )
{
  for(size_t s = 0; s < text.size(); s++)
  {
    if (!isWhitespace(text[s]))
    {
      if (s)
	text.erase(text.begin()+s-1);
      return;
    }
  }
}

std::string trimLeadingWhitespace ( const std::string &text )
{
  std::string s = text;
  trimLeadingWhitespace(s);
  return s;
}



// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8
<commit_msg>Fix shadows<commit_after>/* bzflag
* Copyright (c) 1993 - 2008 <NAME>
*
* This package is free software;  you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <stdio.h>
#include <stdarg.h>
#include <sstream>
#include "plugin_utils.h"

const char* bzu_GetTeamName( bz_eTeamType team )
{
  switch (team){

  case eRedTeam:
    return "Red";

  case eGreenTeam:
    return "Green";

  case eBlueTeam:
    return "Blue";

  case ePurpleTeam:
    return "Purple";

  case eRogueTeam:
    return "Rogue";

  case eObservers:
    return "Observer";

  case eRabbitTeam:
    return "Rabbit";

  case eHunterTeam:
    return "Hunter";

  default:
    break;
  }

  return "Unknown";
}

std::string printTime ( bz_Time *ts, const char* _timezone )
{
  std::string time;
  appendTime(time,ts,_timezone);
  return time;
}

//Date: Mon, 23 Jun 2008 17:50:22 GMT

void appendTime ( std::string & text, bz_Time *ts, const char* _timezone )
{
  switch(ts->dayofweek)
  {
    case 1:
      text += "Mon";
      break;
    case 2:
      text += "Tue";
      break;
    case 3:
      text += "Wed";
      break;
    case 4:
      text += "Thu";
      break;
    case 5:
      text += "Fri";
      break;
    case 6:
      text += "Sat";
      break;
    case 0:
      text += "Sun";
      break;
  }

  text += format(", %d ",ts->day);
  
  switch(ts->month)
  {
  case 0:
    text += "Jan";
    break;
  case 1:
    text += "Feb";
    break;
  case 2:
    text += "Mar";
    break;
  case 3:
    text += "Apr";
    break;
  case 4:
    text += "May";
    break;
  case 5:
    text += "Jun";
    break;
  case 6:
    text += "Jul";
    break;
  case 7:
    text += "Aug";
    break;
  case 8:
    text += "Sep";
    break;
  case 9:
    text += "Oct";
    break;
  case 10:
    text += "Nov";
    break;
  case 11:
    text += "Dec";
    break;
 }

  text += format(" %d %d:%d:%d ",ts->year,ts->hour,ts->minute,ts->second);
  if (_timezone)
    text += _timezone;
  else
    text += "GMT";
}

std::string no_whitespace(const std::string &s)
{
  const int sourcesize = (int)s.size();

  int count = 0, i = 0, j = 0;
  for (i = 0; i < sourcesize; i++)
    if (!isWhitespace(s[i]))
      count++;

  // create result string of correct size
  std::string result(count, ' ');

  for (i = 0, j = 0; i < sourcesize; i++)
    if (!isWhitespace(s[i]))
      result[j++] = s[i];

  return result;
}

std::string tolower(const std::string& s)
{
  std::string trans = s;

  for (std::string::iterator i=trans.begin(), end=trans.end(); i!=end; ++i)
    *i = ::tolower(*i);
  return trans;
}

std::string tolower(const char* s)
{
  std::string trans = s;

  for (std::string::iterator i=trans.begin(), end=trans.end(); i!=end; ++i)
    *i = ::tolower(*i);
  return trans;
}

std::string format(const char* fmt, ...)
{
  va_list args;
  va_start(args, fmt);
  char	temp[2048];
  vsprintf(temp,fmt, args);
  std::string result = temp;
  va_end(args);
  return result;
}

std::vector<std::string> tokenize(const std::string& in, const std::string &delims, const int maxTokens, const bool useQuotes, size_t offset)
{
  std::vector<std::string> tokens;
  int numTokens = 0;
  bool inQuote = false;

  std::ostringstream currentToken;

  std::string::size_type pos = in.find_first_not_of(delims,offset);
  int currentChar  = (pos == std::string::npos) ? -1 : in[pos];
  bool enoughTokens = (maxTokens && (numTokens >= (maxTokens-1)));

  while (pos != std::string::npos && !enoughTokens) {

    // get next token
    bool tokenDone = false;
    bool foundSlash = false;

    currentChar = (pos < in.size()) ? in[pos] : -1;
    while ((currentChar != -1) && !tokenDone){

      tokenDone = false;

      if (delims.find(currentChar) != std::string::npos && !inQuote) { // currentChar is a delim
	pos ++;
	break; // breaks out of while loop
      }

      if (!useQuotes){
	currentToken << char(currentChar);
      } else {

	switch (currentChar){
	  case '\\' : // found a backslash
	    if (foundSlash){
	      currentToken << char(currentChar);
	      foundSlash = false;
	    } else {
	      foundSlash = true;
	    }
	    break;
	  case '\"' : // found a quote
	    if (foundSlash){ // found \"
	      currentToken << char(currentChar);
	      foundSlash = false;
	    } else { // found unescaped "
	      if (inQuote){ // exiting a quote
		// finish off current token
		tokenDone = true;
		inQuote = false;
		//slurp off one additional delimeter if possible
		if (pos+1 < in.size() &&
		  delims.find(in[pos+1]) != std::string::npos) {
		    pos++;
		  }

	      } else { // entering a quote
		// finish off current token
		tokenDone = true;
		inQuote = true;
	      }
	    }
	    break;
	  default:
	    if (foundSlash){ // don't care about slashes except for above cases
	      currentToken << '\\';
	      foundSlash = false;
	    }
	    currentToken << char(currentChar);
	    break;
	}
      }

      pos++;
      currentChar = (pos < in.size()) ? in[pos] : -1;
    } // end of getting a Token

    if (currentToken.str().size() > 0) { // if the token is something add to list
      tokens.push_back(currentToken.str());
      currentToken.str("");
      numTokens ++;
    }

    enoughTokens = (maxTokens && (numTokens >= (maxTokens-1)));
    if (enoughTokens)
      break;
    else
      pos = in.find_first_not_of(delims,pos);

  } // end of getting all tokens -- either EOL or max tokens reached

  if (enoughTokens && pos != std::string::npos) {
    std::string lastToken = in.substr(pos);
    if (lastToken.size() > 0)
      tokens.push_back(lastToken);
  }

  return tokens;
}

std::string replace_all(const std::string& in, const std::string& replaceMe, const std::string& withMe)
{
  std::string result;
  std::string::size_type beginPos = 0;
  std::string::size_type endPos = 0;
  std::ostringstream tempStream;

  endPos = in.find(replaceMe);
  if (endPos == std::string::npos)
    return in; // can't find anything to replace
  if (replaceMe.empty()) return in; // can't replace nothing with something -- can do reverse

  while (endPos != std::string::npos) {
    // push the  part up to
    tempStream << in.substr(beginPos,endPos-beginPos);
    tempStream << withMe;
    beginPos = endPos + replaceMe.size();
    endPos = in.find(replaceMe,beginPos);
  }
  tempStream << in.substr(beginPos);
  return tempStream.str();
}

std::string url_encode(const std::string &text)
{
  char hex[5];
  std::string destination;
  for (int i=0;  i < (int) text.size(); i++) {
    char c = text[i];
    if (isAlphanumeric(c)) {
      destination+=c;
    } else if (isWhitespace(c)) {
      destination+='+';
    } else {
      destination+='%';
      sprintf(hex, "%-2.2X", c);
      destination.append(hex);
    }
  }
  return destination;
}

std::string url_decode(const std::string &text)
{
  std::string destination;

  std::string::const_iterator itr = text.begin();
  while ( itr != text.end() ) {
    if (*itr != '%') {
      destination += *itr++;
    } else {
      char hex[5] = "0x00";

      itr++;;
      if (itr == text.end())
	return destination;

      hex[2] = *itr;

      itr++;;
      if (itr == text.end())
	return destination;

      hex[3] = *itr;

      int val = 0;
      sscanf(hex,"%x",&val);
      if ( val != 0 )
	destination += (char)val;
      itr++;
    }
  }
  return destination;
}

size_t find_first_substr(const std::string &findin, const std::string findwhat, size_t offset)
{
  if (findwhat.size())
  {
    for ( size_t f = offset; f < findin.size(); f++ )
    {
      if (findin[f] == findwhat[0])
      {
	size_t start = f;
	for ( size_t w = 1; w < findwhat.size(); w++ )
	{
	  if ( f+w > findin.size())
	    return std::string::npos;
	  if (findin[f+w] != findwhat[w])
	  {
	    f+=w;
	    w = findwhat.size();
	  }
	}
	if (start == f)
	  return f;
      }
    }
  }
  return std::string::npos;
}

void trimLeadingWhitespace ( std::string &text )
{
  for(size_t s = 0; s < text.size(); s++)
  {
    if (!isWhitespace(text[s]))
    {
      if (s)
	text.erase(text.begin()+s-1);
      return;
    }
  }
}

std::string trimLeadingWhitespace ( const std::string &text )
{
  std::string s = text;
  trimLeadingWhitespace(s);
  return s;
}



// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8

{'fraction_non_alphanumeric': 0.16237876764039724, 'fraction_numerical': 0.009756664150066787, 'mean_word_length': 2.6467598475222363, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 39, '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': '2476466', 'n_tokens_mistral': 6419, 'n_tokens_neox': 6112, 'n_words': 1637}