<commit_before>/*
 *  Copyright (c) 2016, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */
#pragma once

#include <sys/time.h>

#include <chrono>
#include <string>

#include <folly/Format.h>
#include <folly/Likely.h>
#include <folly/Range.h>

#include "mcrouter/lib/fbi/nstring.h"

using timeval_t = struct timeval;

namespace folly {
struct dynamic;
} // folly

namespace facebook { namespace memcache {

/**
 * If `condition` is false, throws std::logic_error.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
void checkLogic(bool condition, folly::StringPiece fmt, Args&&... args) {
  if (UNLIKELY(!condition)) {
    throw std::logic_error(folly::sformat(fmt, std::forward<Args>(args)...));
  }
}

/**
 * Throws std::logic_error with formatted string.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
[[ noreturn ]] void throwLogic(folly::StringPiece fmt, Args&&... args) {
  throw std::logic_error(folly::sformat(fmt, std::forward<Args>(args)...));
}

/**
 * If `condition` is false, throws std::runtime_error.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
void checkRuntime(bool condition, folly::StringPiece fmt, Args&&... args) {
  if (UNLIKELY(!condition)) {
    throw std::runtime_error(folly::sformat(fmt, std::forward<Args>(args)...));
  }
}

/**
 * Throws std::runtime_error with formatted string.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
[[ noreturn ]] void throwRuntime(folly::StringPiece fmt, Args&&... args) {
  throw std::runtime_error(folly::sformat(fmt, std::forward<Args>(args)...));
}

/** folly::to style conversion routines */

template <typename T, typename F> T to(const F& x);

template <>
inline nstring_t to<nstring_t>(const folly::StringPiece& sp) {
  nstring_t ns;
  ns.str = (sp.empty() ? nullptr : (char*)sp.begin());
  ns.len = sp.size();
  return ns;
}

template <>
inline nstring_t to<nstring_t>(const std::string& s) {
  nstring_t ns;
  ns.str = (s.empty() ? nullptr : (char*)s.data());
  ns.len = s.size();
  return ns;
}

template <>
inline folly::StringPiece to<folly::StringPiece>(const nstring_t& ns) {
  return folly::StringPiece(ns.str, ns.len);<commit_msg>Extensibility for folly::to<> through ADL<commit_after>/*
 *  Copyright (c) 2016, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */
#pragma once

#include <sys/time.h>

#include <chrono>
#include <string>

#include <folly/Format.h>
#include <folly/Likely.h>
#include <folly/Range.h>

#include "mcrouter/lib/fbi/nstring.h"

using timeval_t = struct timeval;

namespace folly {
struct dynamic;
} // folly

inline void parseTo(folly::StringPiece sp, nstring_t& ns) {
  ns.str = (sp.empty() ? nullptr : (char*)sp.begin());
  ns.len = sp.size();
}


namespace facebook { namespace memcache {

/**
 * If `condition` is false, throws std::logic_error.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
void checkLogic(bool condition, folly::StringPiece fmt, Args&&... args) {
  if (UNLIKELY(!condition)) {
    throw std::logic_error(folly::sformat(fmt, std::forward<Args>(args)...));
  }
}

/**
 * Throws std::logic_error with formatted string.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
[[ noreturn ]] void throwLogic(folly::StringPiece fmt, Args&&... args) {
  throw std::logic_error(folly::sformat(fmt, std::forward<Args>(args)...));
}

/**
 * If `condition` is false, throws std::runtime_error.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
void checkRuntime(bool condition, folly::StringPiece fmt, Args&&... args) {
  if (UNLIKELY(!condition)) {
    throw std::runtime_error(folly::sformat(fmt, std::forward<Args>(args)...));
  }
}

/**
 * Throws std::runtime_error with formatted string.
 * `fmt` and `args` are passed to folly::format().
 */
template <typename... Args>
[[ noreturn ]] void throwRuntime(folly::StringPiece fmt, Args&&... args) {
  throw std::runtime_error(folly::sformat(fmt, std::forward<Args>(args)...));
}

/** folly::to style conversion routines */

template <typename T, typename F> T to(const F& x);

template <>
inline nstring_t to<nstring_t>(const folly::StringPiece& sp) {
  nstring_t ns;
  parseTo(sp, ns);
  return ns;
}

template <>
inline nstring_t to<nstring_t>(const std::string& s) {
  nstring_t ns;
  parseTo(s, ns);
  return ns;
}

template <>
inline folly::StringPiece to<folly::StringPiece>(const nstring_t& ns) {
  return folly::StringPiece(ns.str, ns.len);
{'fraction_non_alphanumeric': 0.16713203031511767, 'fraction_numerical': 0.0015955325089748703, 'mean_word_length': 4.380901287553648, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3598028', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1723, 'n_words': 477}