<commit_before>/**
 * @file       BlynkUtility.h
 * @author     <NAME>
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 <NAME>
 * @date       Jun 2015
 * @brief      Utility functions
 *
 */

#ifndef BlynkUtility_h
#define BlynkUtility_h

template<class T>
const T& BlynkMin(const T& a, const T& b)
{
    return (b < a) ? b : a;
}

template<class T>
const T& BlynkMax(const T& a, const T& b)
{
    return (b < a) ? a : b;
}


template <class T, class T2>
T BlynkMathMap(T x, T2 in_min, T2 in_max, T2 out_min, T2 out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

template <class T, class T2>
T BlynkMathClamp(T val, T2 low, T2 high)
{
  return (val < low) ? low : ((val > high) ? high : val);
}

template <class T, class T2>
T BlynkMathClampMap(T x, T2 in_min, T2 in_max, T2 out_min, T2 out_max)
{
  x = BlynkMathClamp(x, in_min, in_max);
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


template <unsigned WSIZE, typename T>
void BlynkAverageSample (T& avg, const T& input) {
    avg -= avg/WSIZE;
    const T add = input/WSIZE;
    // Fix for shorter delays
    if (add > 0)
      avg += add;
    else
      avg -= 1;
}

static inline
uint32_t BlynkCRC32(const void* data, size_t length, uint32_t previousCrc32 = 0)
{
  const uint32_t Polynomial = 0xEDB88320;
  uint32_t crc = ~previousCrc32;
  unsigned char* current = (unsigned char*)data;
  while (length--) {
    crc ^= *current++;
    for (unsigned int j = 0; j < 8; j++) {
      crc = (crc >> 1) ^ (-int(crc & 1) & Polynomial);
    }
  }
  return ~crc;
}

static inline
bool BlynkStrMatch(const char* pat, const char* txt) {
    const long n = strlen(txt);
    const long m = strlen(pat);

    if (m == 0)
        return (n == 0);

    long i = 0, j = 0, index_txt = -1,
        index_pat = -1;

    while (i < n) {
        if (j < m && txt[i] == pat[j]) {
            i++;
            j++;
        } else if (j < m && pat[j] == '?') {
            i++;
            j++;
        } else if (j < m && pat[j] == '*') {
            index_txt = i;
            index_pat = j;<commit_msg>Update BlynkUtility.h<commit_after>/**
 * @file       BlynkUtility.h
 * @author     <NAME>
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 <NAME>
 * @date       Jun 2015
 * @brief      Utility functions
 *
 */

#ifndef BlynkUtility_h
#define BlynkUtility_h

template<class T>
const T& BlynkMin(const T& a, const T& b)
{
    return (b < a) ? b : a;
}

template<class T>
const T& BlynkMax(const T& a, const T& b)
{
    return (b < a) ? a : b;
}


template <class T, class T2>
T BlynkMathMap(T x, T2 in_min, T2 in_max, T2 out_min, T2 out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

template <class T, class T2>
T BlynkMathClamp(T val, T2 low, T2 high)
{
  return (val < low) ? low : ((val > high) ? high : val);
}

template <class T, class T2>
T BlynkMathClampMap(T x, T2 in_min, T2 in_max, T2 out_min, T2 out_max)
{
  x = BlynkMathClamp(x, in_min, in_max);
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


template <unsigned WSIZE, typename T>
void BlynkAverageSample (T& avg, const T& input) {
    avg -= avg/WSIZE;
    const T add = input/WSIZE;
    // Fix for shorter delays
    if (add > 0)
      avg += add;
    else
      avg -= 1;
}

static
int BlynkRSSI2SQ(int dBm) {
    if(dBm <= -100) {
        return 0;
    } else if(dBm >= -50) {
        return 100;
    } else {
        return 2 * (dBm + 100);
    }
}

static
int BlynkSQ2RSSI(int quality) {
    if(quality <= 0) {
        return -100;
    } else if(quality >= 100) {
        return -50;
    } else {
        return (quality / 2) - 100;
    }
}

static
uint32_t BlynkCRC32(const void* data, size_t length, uint32_t previousCrc32 = 0)
{
  const uint32_t Polynomial = 0xEDB88320;
  uint32_t crc = ~previousCrc32;
  unsigned char* current = (unsigned char*)data;
  while (length--) {
    crc ^= *current++;
    for (unsigned int j = 0; j < 8; j++) {
      crc = (crc >> 1) ^ (-int(crc & 1) & Polynomial);
    }
  }
  return ~crc;
}

static
bool BlynkStrMatch(const char* pat, const char* txt) {
    const long n = strlen(txt);
    const long m = strlen(pat);

    if (m == 0)
        return (n == 0);

    long i = 0, j = 0, index_txt = -1,
        index_pat = -1;

    while (i < n) {
        if (j < m && txt[i] == pat[j]) {
            i++;
            j++;
        } else if (j < m && pat[j] == '?') {
            i++;
            j++;
        } else if (j < m && pat[j] == '*') {
            index_txt = i;
            index_pat = j;
{'fraction_non_alphanumeric': 0.14758650149508756, 'fraction_numerical': 0.029047415634344296, 'mean_word_length': 2.2817098808689558, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 29, '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': '7357789', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1861, 'n_words': 560}