text:
// Copyright 2003-2009 The RE2 Authors.  All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Regular expression interface RE2.
//
// Originally the PCRE C++ wrapper, but adapted to use
// the new automata-based regular expression engines.

#include "re2/re2.h"

#include <stdio.h>
#include <string>
#include <pthread.h>
#include <errno.h>
#include "util/atomicops.h"
#include "util/util.h"
#include "util/flags.h"
#include "util/sparse_array.h"
#include "re2/prog.h"
#include "re2/regexp.h"

DEFINE_bool(trace_re2, false, "trace RE2 execution");

namespace re2 {

// Maximum number of args we can set
static const int kMaxArgs = 16;
static const int kVecSize = 1+kMaxArgs;

const VariadicFunction2<bool, const StringPiece&, const RE2&, RE2::Arg, RE2::FullMatchN> RE2::FullMatch = {};
const VariadicFunction2<bool, const StringPiece&, const RE2&, RE2::Arg, RE2::PartialMatchN> RE2::PartialMatch = {};
const VariadicFunction2<bool, StringPiece*, const RE2&, RE2::Arg, RE2::ConsumeN> RE2::Consume = {};
const VariadicFunction2<bool, StringPiece*, const RE2&, RE2::Arg, RE2::FindAndConsumeN> RE2::FindAndConsume = {};

// This will trigger LNK2005 error in MSVC.
#ifndef COMPILER_MSVC
const int RE2::Options::kDefaultMaxMem;  // initialized in re2.h
#endif  // COMPILER_MSVC

RE2::Options::Options(RE2::CannedOptions opt)
  : encoding_(opt == RE2::Latin1 ? EncodingLatin1 : EncodingUTF8),
    posix_syntax_(opt == RE2::POSIX),
    longest_match_(opt == RE2::POSIX),
    log_errors_(opt != RE2::Quiet),
    max_mem_(kDefaultMaxMem),
    literal_(false),
    never_nl_(false),
    dot_nl_(false),
    never_capture_(false),
    case_sensitive_(true),
    perl_classes_(false),
    word_boundary_(false),
    one_line_(false) {
}

// static empty things for use as const references.
// To avoid global constructors, initialized on demand.
GLOBAL_MUTEX(empty_mutex);
static const string *empty_string;
static const map<string, int> *empty_named_groups;
static const map<int, string> *empty_group_names;

static void InitEmpty() {
  GLOBAL_MUTEX_LOCK(empty_mutex);
  if (empty_string == NULL) {
    empty_string = new string;
    empty_named_groups = new map<string, int>;
    empty_group_names = new map<int, string>;
  }
  GLOBAL_MUTEX_UNLOCK(empty_mutex);
}

// Converts from Regexp error code to RE2 error code.
// Maybe some day they will diverge.  In any event, this
// hides the existence of Regexp from RE2 users.
static RE2::ErrorCode RegexpErrorToRE2(re2::RegexpStatusCode code) {
  switch (code) {
    case re2::kRegexpSuccess:
      return RE2::NoError;
    case re2::kRegexpInternalError:
      return RE2::ErrorInternal;
    case re2::kRegexpBadEscape:
      return RE2::ErrorBadEscape;
    case re2::kRegexpBadCharClass:
      return RE2::ErrorBadCharClass;
    case re2::kRegexpBadCharRange:
      return RE2::ErrorBadCharRange;
    case re2::kRegexpMissingBracket:
      return RE2::ErrorMissingBracket;
    case re2::kRegexpMissingParen:
      return RE2::ErrorMissingParen;
    case re2::kRegexpTrailingBackslash:
      return RE2::ErrorTrailingBackslash;
    case re2::kRegexpRepeatArgument:
      return RE2::ErrorRepeatArgument;
    case re2::kRegexpRepeatSize:
      return RE2::ErrorRepeatSize;
    case re2::kRegexpRepeatOp:
      return RE2::ErrorRepeatOp;
    case re2::kRegexpBadPerlOp:
      return RE2::ErrorBadPerlOp;
    case re2::kRegexpBadUTF8:
      return RE2::ErrorBadUTF8;
    case re2::kRegexpBadNamedCapture:
      return RE2::ErrorBadNamedCapture;
  }
  return RE2::ErrorInternal;
}

static string trunc(const StringPiece& pattern) {
  if (pattern.size() < 100)
    return pattern.as_string();
  return pattern.substr(0, 100).as_string() + "...";
}

RE2::RE2(const char* pattern) {
  Init(pattern, DefaultOptions);
}

RE2::RE2(const string& pattern) {
  Init(pattern, DefaultOptions);
}

RE2::RE2(const StringPiece& pattern) {
  Init(pattern, DefaultOptions);
}

RE2::RE2(const StringPiece& pattern, const Options& options) {
  Init(pattern, options);
}

int RE2::Options::ParseFlags() const {
  int flags = Regexp::ClassNL;
  switch (encoding()) {
    default:
      if (log_errors())
        LOG(ERROR) << "Unknown encoding " << encoding();
      break;
    case RE2::Options::EncodingUTF8:
      break;
    case RE2::Options::EncodingLatin1:
      flags |= Regexp::Latin1;
      break;
  }

  if (!posix_syntax())
    flags |= Regexp::LikePerl;

  if (literal())
    flags |= Regexp::Literal;

  if (never_nl())
    flags |= Regexp::NeverNL;

  if (dot_nl())
    flags |= Regexp::DotNL;

  if (never_capture())
    flags |= Regexp::NeverCapture;

  if (!case_sensitive())
    flags |= Regexp::FoldCase;

  if (perl_classes())
    flags |= Regexp::PerlClasses;

  if (word_boundary())
    flags |= Regexp::PerlB;

  if (one_line())
    flags |= Regexp::OneLine;

  return flags;
}

void RE2::Init(const StringPiece& pattern, const Options& options) {
  mutex_ = new Mutex;
  pattern_ = pattern.as_string();
  options_.Copy(options);
  InitEmpty();
  error_ = empty_string;
  error_code_ = NoError;
  suffix_regexp_ = NULL;
  entire_regexp_ = NULL;
  prog_ = NULL;
  rprog_ = NULL;
  named_groups_ = NULL;
  group_names_ = NULL;
  num_captures_ = -1;

  RegexpStatus status;
  entire_regexp_ = Regexp::Parse(
    pattern_,
    static_cast<Regexp::ParseFlags>(options_.ParseFlags()),
    &status);
  if (entire_regexp_ == NULL) {
    if (error_ == empty_string)
      error_ = new string(status.Text());
    if (options_.log_errors()) {
      LOG(ERROR) << "Error parsing '" << trunc(pattern_) << "': "
                 << status.Text();
    }
    error_arg_ = status.error_arg().as_string();
    error_code_ = RegexpErrorToRE2(status.code());
    return;
  }

  prefix_.clear();
  prefix_foldcase_ = false;
  re2::Regexp* suffix;
  if (entire_regexp_->RequiredPrefix(&prefix_, &prefix_foldcase_, &suffix))
    suffix_regexp_ = suffix;
  else
    suffix_regexp_ = entire_regexp_->Incref();

  // Two thirds of the memory goes to the forward Prog,
  // one third to the reverse prog, because the forward
  // Prog has two DFAs but the reverse prog has one.
  prog_ = suffix_regexp_->CompileToProg(options_.max_mem()*2/3);
  if (prog_ == NULL) {
    if (options_.log_errors())
      LOG(ERROR) << "Error compiling '" << trunc(pattern_) << "'";
    error_ = new string("pattern too large - compile failed");
    error_code_ = RE2::ErrorPatternTooLarge;
    return;
  }

  // Could delay this until the first match call that
  // cares about submatch information, but the one-pass
  // machine's memory gets cut from the DFA memory budget,
  // and that is harder to do if the DFA has already
  // been built.
  is_one_pass_ = prog_->IsOnePass();
}

// Returns rprog_, computing it if needed.
re2::Prog* RE2::ReverseProg() const {
  MutexLock l(mutex_);
  if (rprog_ == NULL && error_ == empty_string) {
    rprog_ = suffix_regexp_->CompileToReverseProg(options_.max_mem()/3);
    if (rprog_ == NULL) {
      if (options_.log_errors())
        LOG(ERROR) << "Error reverse compiling '" << trunc(pattern_) << "'";
      error_ = new string("pattern too large - reverse compile failed");
      error_code_ = RE2::ErrorPatternTooLarge;
      return NULL;
    }
  }
  return rprog_;
}

RE2::~RE2() {
  if (suffix_regexp_)
    suffix_regexp_->Decref();
  if (entire_regexp_)
    entire_regexp_->Decref();
  delete mutex_;
  delete prog_;
  delete rprog_;
  if (error_ != empty_string)
    delete error_;
  if (named_groups_ != NULL && named_groups_ != empty_named_groups)
    delete named_groups_;
  if (group_names_ != NULL &&  group_names_ != empty_group_names)
    delete group_names_;
}

int RE2::ProgramSize() const {
  if (prog_ == NULL)
    return -1;
  return prog_->size();
}

int RE2::ProgramFanout(map<int, int>* histogram) const {
  if (prog_ == NULL)
    return -1;
  SparseArray<int> fanout(prog_->size());
  prog_->Fanout(&fanout);
  histogram->clear();
  for (SparseArray<int>::iterator i = fanout.begin(); i != fanout.end(); ++i) {
    // TODO(junyer): Optimise this?
    int bucket = 0;
    while (1 << bucket < i->second) {
      bucket++;
    }
    (*histogram)[bucket]++;
  }
  return histogram->rbegin()->first;
}

// Returns named_groups_, computing it if needed.
const map<string, int>&  RE2::NamedCapturingGroups() const {
  MutexLock l(mutex_);
  if (!ok())
    return *empty_named_groups;
  if (named_groups_ == NULL) {
    named_groups_ = suffix_regexp_->NamedCaptures();
    if (named_groups_ == NULL)
      named_groups_ = empty_named_groups;
  }
  return *named_groups_;
}

// Returns group_names_, computing it if needed.
const map<int, string>&  RE2::CapturingGroupNames() const {
  MutexLock l(mutex_);
  if (!ok())
    return *empty_group_names;
  if (group_names_ == NULL) {
    group_names_ = suffix_regexp_->CaptureNames();
    if (group_names_ == NULL)
      group_names_ = empty_group_names;
  }
  return *group_names_;
}

/***** Convenience interfaces *****/

bool RE2::FullMatchN(const StringPiece& text, const RE2& re,
                     const Arg* const args[], int n) {
  return re.DoMatch(text, ANCHOR_BOTH, NULL, args, n);
}

bool RE2::PartialMatchN(const StringPiece& text, const RE2& re,
                        const Arg* const args[], int n) {
  return re.DoMatch(text, UNANCHORED, NULL, args, n);
}

bool RE2::ConsumeN(StringPiece* input, const RE2& re,
                   const Arg* const args[], int n) {
  int consumed;
  if (re.DoMatch(*input, ANCHOR_START, &consumed, args, n)) {
    input->remove_prefix(consumed);
    return true;
  } else {
    return false;
  }
}

bool RE2::FindAndConsumeN(StringPiece* input, const RE2& re,
                          const Arg* const args[], int n) {
  int consumed;
  if (re.DoMatch(*input, UNANCHORED, &consumed, args, n)) {
    input->remove_prefix(consumed);
    return true;
  } else {
    return false;
  }
}

// Returns the maximum submatch needed for the rewrite to be done by Replace().
// E.g. if rewrite == "foo \\2,\\1", returns 2.
int RE2::MaxSubmatch(const StringPiece& rewrite) {
  int max = 0;
  for (const char *s = rewrite.data(), *end = s + rewrite.size();
       s < end; s++) {
    if (*s == '\\') {
      s++;
      int c = (s < end) ? *s : -1;
      if (isdigit(c)) {
        int n = (c - '0');
        if (n > max)
          max = n;
      }
    }
  }
  return max;
}

bool RE2::Replace(string *str,
                 const RE2& re,
                 const StringPiece& rewrite) {
  StringPiece vec[kVecSize];
  int nvec = 1 + MaxSubmatch(rewrite);
  if (nvec > arraysize(vec))
    return false;
  if (!re.Match(*str, 0, str->size(), UNANCHORED, vec, nvec))
    return false;

  string s;
  if (!re.Rewrite(&s, rewrite, vec, nvec))
    return false;

  assert(vec[0].begin() >= str->data());
  assert(vec[0].end() <= str->data()+str->size());
  str->replace(vec[0].data() - str->data(), vec[0].size(), s);
  return true;
}

int RE2::GlobalReplace(string *str,
                      const RE2& re,
                      const StringPiece& rewrite) {
  StringPiece vec[kVecSize];
  int nvec = 1 + MaxSubmatch(rewrite);
  if (nvec > arraysize(vec))
    return false;

  const char* p = str->data();
  const char* ep = p + str->size();
  const char* lastend = NULL;
  string out;
  int count = 0;
  while (p <= ep) {
    if (!re.Match(*str, p - str->data(), str->size(), UNANCHORED, vec, nvec))
      break;
    if (p < vec[0].begin())
      out.append(p, vec[0].begin() - p);
    if (vec[0].begin() == lastend && vec[0].size() == 0) {
      // Disallow empty match at end of last match: skip ahead.
      if (p < ep)
        out.append(p, 1);
      p++;
      continue;
    }
    re.Rewrite(&out, rewrite, vec, nvec);
    p = vec[0].end();
    lastend = p;
    count++;
  }

  if (count == 0)
    return 0;

  if (p < ep)
    out.append(p, ep - p);
  swap(out, *str);
  return count;
}

bool RE2::Extract(const StringPiece &text,
                 const RE2& re,
                 const StringPiece &rewrite,
                 string *out) {
  StringPiece vec[kVecSize];
  int nvec = 1 + MaxSubmatch(rewrite);
  if (nvec > arraysize(vec))
    return false;

  if (!re.Match(text, 0, text.size(), UNANCHORED, vec, nvec))
    return false;

  out->clear();
  return re.Rewrite(out, rewrite, vec, nvec);
}

string RE2::QuoteMeta(const StringPiece& unquoted) {
  string result;
  result.reserve(unquoted.size() << 1);

  // Escape any ascii character not in [A-Za-z_0-9].
  //
  // Note that it's legal to escape a character even if it has no
  // special meaning in a regular expression -- so this function does
  // that.  (This also makes it identical to the perl function of the
  // same name except for the null-character special case;
  // see `perldoc -f quotemeta`.)
  for (int ii = 0; ii < unquoted.length(); ++ii) {
    // Note that using 'isalnum' here raises the benchmark time from
    // 32ns to 58ns:
    if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
        (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
        (unquoted[ii] < '0' || unquoted[ii] > '9') &&
        unquoted[ii] != '_' &&
        // If this is the part of a UTF8 or Latin1 character, we need
        // to copy this byte without escaping.  Experimentally this is
        // what works correctly with the regexp library.
        !(unquoted[ii] & 128)) {
      if (unquoted[ii] == '\0') {  // Special handling for null chars.
        // Note that this special handling is not strictly required for RE2,
        // but this quoting is required for other regexp libraries such as
        // PCRE.
        // Can't use "\\0" since the next character might be a digit.
        result += "\\x00";
        continue;
      }
      result += '\\';
    }
    result += unquoted[ii];
  }

  return result;
}

bool RE2::PossibleMatchRange(string* min, string* max, int maxlen) const {
  if (prog_ == NULL)
    return false;

  int n = prefix_.size();
  if (n > maxlen)
    n = maxlen;

  // Determine initial min max from prefix_ literal.
  string pmin, pmax;
  pmin = prefix_.substr(0, n);
  pmax = prefix_.substr(0, n);
  if (prefix_foldcase_) {
    // prefix is ASCII lowercase; change pmin to uppercase.
    for (int i = 0; i < n; i++) {
      if ('a' <= pmin[i] && pmin[i] <= 'z')
        pmin[i] += 'A' - 'a';
    }
  }

  // Add to prefix min max using PossibleMatchRange on regexp.
  string dmin, dmax;
  maxlen -= n;
  if (maxlen > 0 && prog_->PossibleMatchRange(&dmin, &dmax, maxlen)) {
    pmin += dmin;
    pmax += dmax;
  } else if (pmax.size() > 0) {
    // prog_->PossibleMatchRange has failed us,
    // but we still have useful information from prefix_.
    // Round up pmax to allow any possible suffix.
    pmax = PrefixSuccessor(pmax);
  } else {
    // Nothing useful.
    *min = "";
    *max = "";
    return false;
  }

  *min = pmin;
  *max = pmax;
  return true;
}

// Avoid possible locale nonsense in standard strcasecmp.
// The string a is known to be all lowercase.
static int ascii_strcasecmp(const char* a, const char* b, int len) {
  const char *ae = a + len;

  for (; a < ae; a++, b++) {
    uint8 x = *a;
    uint8 y = *b;
    if ('A' <= y && y <= 'Z')
      y += 'a' - 'A';
    if (x != y)
      return x - y;
  }
  return 0;
}

/***** Actual matching and rewriting code *****/

bool RE2::Match(const StringPiece& text,
                int startpos,
                int endpos,
                Anchor re_anchor,
                StringPiece* submatch,
                int nsubmatch) const {
  if (!ok() || suffix_regexp_ == NULL) {
    if (options_.log_errors())
      LOG(ERROR) << "Invalid RE2: " << *error_;
    return false;
  }

  if (startpos < 0 || startpos > endpos || endpos > text.size()) {
    if (options_.log_errors())
      LOG(ERROR) << "RE2: invalid startpos, endpos pair. ["
                 << "startpos: " << startpos << ", "
                 << "endpos: " << endpos << ", "
                 << "text size: " << text.size() << "]";
    return false;
  }

  StringPiece subtext = text;
  subtext.remove_prefix(startpos);
  subtext.remove_suffix(text.size() - endpos);

  // Use DFAs to find exact location of match, filter out non-matches.

  // Don't ask for the location if we won't use it.
  // SearchDFA can do extra optimizations in that case.
  StringPiece match;
  StringPiece* matchp = &match;
  if (nsubmatch == 0)
    matchp = NULL;

  int ncap = 1 + NumberOfCapturingGroups();
  if (ncap > nsubmatch)
    ncap = nsubmatch;

  // If the regexp is anchored explicitly, must not be in middle of text.
  if (prog_->anchor_start() && startpos != 0)
    return false;

  // If the regexp is anchored explicitly, update re_anchor
  // so that we can potentially fall into a faster case below.
  if (prog_->anchor_start() && prog_->anchor_end())
    re_anchor = ANCHOR_BOTH;
  else if (prog_->anchor_start() && re_anchor != ANCHOR_BOTH)
    re_anchor = ANCHOR_START;

  // Check for the required prefix, if any.
  int prefixlen = 0;
  if (!prefix_.empty()) {
    if (startpos != 0)
      return false;
    prefixlen = prefix_.size();
    if (prefixlen > subtext.size())
      return false;
    if (prefix_foldcase_) {
      if (ascii_strcasecmp(&prefix_[0], subtext.data(), prefixlen) != 0)
        return false;
    } else {
      if (memcmp(&prefix_[0], subtext.data(), prefixlen) != 0)
        return false;
    }
    subtext.remove_prefix(prefixlen);
    // If there is a required prefix, the anchor must be at least ANCHOR_START.
    if (re_anchor != ANCHOR_BOTH)
      re_anchor = ANCHOR_START;
  }

  Prog::Anchor anchor = Prog::kUnanchored;
  Prog::MatchKind kind = Prog::kFirstMatch;
  if (options_.longest_match())
    kind = Prog::kLongestMatch;
  bool skipped_test = false;

  bool can_one_pass = (is_one_pass_ && ncap <= Prog::kMaxOnePassCapture);

  // SearchBitState allocates a bit vector of size prog_->size() * text.size().
  // It also allocates a stack of 3-word structures which could potentially
  // grow as large as prog_->size() * text.size() but in practice is much
  // smaller.
  // Conditions for using SearchBitState:
  const int MaxBitStateProg = 500;   // prog_->size() <= Max.
  const int MaxBitStateVector = 256*1024;  // bit vector size <= Max (bits)
  bool can_bit_state = prog_->size() <= MaxBitStateProg;
  int bit_state_text_max = MaxBitStateVector / prog_->size();

  bool dfa_failed = false;
  switch (re_anchor) {
    default:
    case UNANCHORED: {
      if (!prog_->SearchDFA(subtext, text, anchor, kind,
                            matchp, &dfa_failed, NULL)) {
        if (dfa_failed) {
          // Fall back to NFA below.
          skipped_test = true;
          if (FLAGS_trace_re2)
            LOG(INFO) << "Match " << trunc(pattern_)
                      << " [" << CEscape(subtext) << "]"
                      << " DFA failed.";
          break;
        }
        if (FLAGS_trace_re2)
          LOG(INFO) << "Match " << trunc(pattern_)
                    << " [" << CEscape(subtext) << "]"
                    << " used DFA - no match.";
        return false;
      }
      if (FLAGS_trace_re2)
        LOG(INFO) << "Match " << trunc(pattern_)
                  << " [" << CEscape(subtext) << "]"
                  << " used DFA - match";
      if (matchp == NULL)  // Matched.  Don't care where
        return true;
      // SearchDFA set match[0].end() but didn't know where the
      // match started.  Run the regexp backward from match[0].end()
      // to find the longest possible match -- that's where it started.
      Prog* prog = ReverseProg();
      if (prog == NULL)
        return false;
      if (!prog->SearchDFA(match, text, Prog::kAnchored,
                           Prog::kLongestMatch, &match, &dfa_failed, NULL)) {
        if (dfa_failed) {
          // Fall back to NFA below.
          skipped_test = true;
          if (FLAGS_trace_re2)
            LOG(INFO) << "Match " << trunc(pattern_)
                      << " [" << CEscape(subtext) << "]"
                      << " reverse DFA failed.";
          break;
        }
        if (FLAGS_trace_re2)
          LOG(INFO) << "Match " << trunc(pattern_)
                    << " [" << CEscape(subtext) << "]"
                    << " DFA inconsistency.";
        if (options_.log_errors())
          LOG(ERROR) << "DFA inconsistency";
        return false;
      }
      if (FLAGS_trace_re2)
        LOG(INFO) << "Match " << trunc(pattern_)
                  << " [" << CEscape(subtext) << "]"
                  << " used reverse DFA.";
      break;
    }

    case ANCHOR_BOTH:
    case ANCHOR_START:
      if (re_anchor == ANCHOR_BOTH)
        kind = Prog::kFullMatch;
      anchor = Prog::kAnchored;

      // If only a small amount of text and need submatch
      // information anyway and we're going to use OnePass or BitState
      // to get it, we might as well not even bother with the DFA:
      // OnePass or BitState will be fast enough.
      // On tiny texts, OnePass outruns even the DFA, and
      // it doesn't have the shared state and occasional mutex that
      // the DFA does.
      if (can_one_pass && text.size() <= 4096 &&
          (ncap > 1 || text.size() <= 8)) {
        if (FLAGS_trace_re2)
          LOG(INFO) << "Match " << trunc(pattern_)
                    << " [" << CEscape(subtext) << "]"
                    << " skipping DFA for OnePass.";
        skipped_test = true;
        break;
      }
      if (can_bit_state && text.size() <= bit_state_text_max && ncap > 1) {
        if (FLAGS_trace_re2)
          LOG(INFO) << "Match " << trunc(pattern_)
                    << " [" << CEscape(subtext) << "]"
                    << " skipping DFA for BitState.";
        skipped_test = true;
        break;
      }
      if (!prog_->SearchDFA(subtext, text, anchor, kind,
                            &match, &dfa_failed, NULL)) {
        if (dfa_failed) {
          if (FLAGS_trace_re2)
            LOG(INFO) << "Match " << trunc(pattern_)
                      << " [" << CEscape(subtext) << "]"
                      << " DFA failed.";
          skipped_test = true;
          break;
        }
        if (FLAGS_trace_re2)
          LOG(INFO) << "Match " << trunc(pattern_)
                    << " [" << CEscape(subtext) << "]"
                    << " used DFA - no match.";
        return false;
      }
      break;
  }

  if (!skipped_test && ncap <= 1) {
    // We know exactly where it matches.  That's enough.
    if (ncap == 1)
      submatch[0] = match;
  } else {
    StringPiece subtext1;
    if (skipped_test) {
      // DFA ran out of memory or was skipped:
      // need to search in entire original text.
      subtext1 = subtext;
    } else {
      // DFA found the exact match location:
      // let NFA run an anchored, full match search
      // to find submatch locations.
      subtext1 = match;
      anchor = Prog::kAnchored;
      kind = Prog::kFullMatch;
    }

    if (can_one_pass && anchor != Prog::kUnanchored) {
      if (FLAGS_trace_re2)
        LOG(INFO) << "Match " << trunc(pattern_)
                  << " [" << CEscape(subtext) << "]"
                  << " using OnePass.";
      if (!prog_->SearchOnePass(subtext1, text, anchor, kind, submatch, ncap)) {
        if (!skipped_test && options_.log_errors())
          LOG(ERROR) << "SearchOnePass inconsistency";
        return false;
      }
    } else if (can_bit_state && subtext1.size() <= bit_state_text_max) {
      if (FLAGS_trace_re2)
        LOG(INFO) << "Match " << trunc(pattern_)
                  << " [" << CEscape(subtext) << "]"
                  << " using BitState.";
      if (!prog_->SearchBitState(subtext1, text, anchor,
                                 kind, submatch, ncap)) {
        if (!skipped_test && options_.log_errors())
          LOG(ERROR) << "SearchBitState inconsistency";
        return false;
      }
    } else {
      if (FLAGS_trace_re2)
        LOG(INFO) << "Match " << trunc(pattern_)
                  << " [" << CEscape(subtext) << "]"
                  << " using NFA.";
      if (!prog_->SearchNFA(subtext1, text, anchor, kind, submatch, ncap)) {
        if (!skipped_test && options_.log_errors())
          LOG(ERROR) << "SearchNFA inconsistency";
        return false;
      }
    }
  }

  // Adjust overall match for required prefix that we stripped off.
  if (prefixlen > 0 && nsubmatch > 0)
    submatch[0] = StringPiece(submatch[0].begin() - prefixlen,
                              submatch[0].size() + prefixlen);

  // Zero submatches that don't exist in the regexp.
  for (int i = ncap; i < nsubmatch; i++)
    submatch[i] = NULL;
  return true;
}

// Internal matcher - like Match() but takes Args not StringPieces.
bool RE2::DoMatch(const StringPiece& text,
                  Anchor anchor,
                  int* consumed,
                  const Arg* const* args,
                  int n) const {
  if (!ok()) {
    if (options_.log_errors())
      LOG(ERROR) << "Invalid RE2: " << *error_;
    return false;
  }

  // Count number of capture groups needed.
  int nvec;
  if (n == 0 && consumed == NULL)
    nvec = 0;
  else
    nvec = n+1;

  StringPiece* vec;
  StringPiece stkvec[kVecSize];
  StringPiece* heapvec = NULL;

  if (nvec <= arraysize(stkvec)) {
    vec = stkvec;
  } else {
    vec = new StringPiece[nvec];
    heapvec = vec;
  }

  if (!Match(text, 0, text.size(), anchor, vec, nvec)) {
    delete[] heapvec;
    return false;
  }

  if(consumed != NULL)
    *consumed = vec[0].end() - text.begin();

  if (n == 0 || args == NULL) {
    // We are not interested in results
    delete[] heapvec;
    return true;
  }

  int ncap = NumberOfCapturingGroups();
  if (ncap < n) {
    // RE has fewer capturing groups than number of arg pointers passed in
    VLOG(1) << "Asked for " << n << " but only have " << ncap;
    delete[] heapvec;
    return false;
  }

  // If we got here, we must have matched the whole pattern.
  for (int i = 0; i < n; i++) {
    const StringPiece& s = vec[i+1];
    if (!args[i]->Parse(s.data(), s.size())) {
      // TODO: Should we indicate what the error was?
      VLOG(1) << "Parse error on #" << i << " " << s << " "
              << (void*)s.data() << "/" << s.size();
      delete[] heapvec;
      return false;
    }
  }

  delete[] heapvec;
  return true;
}

// Append the "rewrite" string, with backslash subsitutions from "vec",
// to string "out".
bool RE2::Rewrite(string *out, const StringPiece &rewrite,
                 const StringPiece *vec, int veclen) const {
  for (const char *s = rewrite.data(), *end = s + rewrite.size();
       s < end; s++) {
    int c = *s;
    if (c == '\\') {
      s++;
      c = (s < end) ? *s : -1;
      if (isdigit(c)) {
        int n = (c - '0');
        if (n >= veclen) {
          if (options_.log_errors()) {
            LOG(ERROR) << "requested group " << n
                       << " in regexp " << rewrite.data();
          }
          return false;
        }
        StringPiece snip = vec[n];
        if (snip.size() > 0)
          out->append(snip.data(), snip.size());
      } else if (c == '\\') {
        out->push_back('\\');
      } else {
        if (options_.log_errors())
          LOG(ERROR) << "invalid rewrite pattern: " << rewrite.data();
        return false;
      }
    } else {
      out->push_back(c);
    }
  }
  return true;
}

// Return the number of capturing subpatterns, or -1 if the
// regexp wasn't valid on construction.
int RE2::NumberOfCapturingGroups() const {
  if (suffix_regexp_ == NULL)
    return -1;
  int n;
  ATOMIC_LOAD_RELAXED(n, &num_captures_);
  if (n == -1) {
    n = suffix_regexp_->NumCaptures();
    ATOMIC_STORE_RELAXED(&num_captures_, n);
  }
  return n;
}

// Checks that the rewrite string is well-formed with respect to this
// regular expression.
bool RE2::CheckRewriteString(const StringPiece& rewrite, string* error) const {
  int max_token = -1;
  for (const char *s = rewrite.data(), *end = s + rewrite.size();
       s < end; s++) {
    int c = *s;
    if (c != '\\') {
      continue;
    }
    if (++s == end) {
      *error = "Rewrite schema error: '\\' not allowed at end.";
      return false;
    }
    c = *s;
    if (c == '\\') {
      continue;
    }
    if (!isdigit(c)) {
      *error = "Rewrite schema error: "
               "'\\' must be followed by a digit or '\\'.";
      return false;
    }
    int n = (c - '0');
    if (max_token < n) {
      max_token = n;
    }
  }

  if (max_token > NumberOfCapturingGroups()) {
    SStringPrintf(error, "Rewrite schema requests %d matches, "
                  "but the regexp only has %d parenthesized subexpressions.",
                  max_token, NumberOfCapturingGroups());
    return false;
  }
  return true;
}

/***** Parsers for various types *****/

bool RE2::Arg::parse_null(const char* str, int n, void* dest) {
  // We fail if somebody asked us to store into a non-NULL void* pointer
  return (dest == NULL);
}

bool RE2::Arg::parse_string(const char* str, int n, void* dest) {
  if (dest == NULL) return true;
  reinterpret_cast<string*>(dest)->assign(str, n);
  return true;
}

bool RE2::Arg::parse_stringpiece(const char* str, int n, void* dest) {
  if (dest == NULL) return true;
  reinterpret_cast<StringPiece*>(dest)->set(str, n);
  return true;
}

bool RE2::Arg::parse_char(const char* str, int n, void* dest) {
  if (n != 1) return false;
  if (dest == NULL) return true;
  *(reinterpret_cast<char*>(dest)) = str[0];
  return true;
}

bool RE2::Arg::parse_uchar(const char* str, int n, void* dest) {
  if (n != 1) return false;
  if (dest == NULL) return true;
  *(reinterpret_cast<unsigned char*>(dest)) = str[0];
  return true;
}

// Largest number spec that we are willing to parse
static const int kMaxNumberLength = 32;

// REQUIRES "buf" must have length at least nbuf.
// Copies "str" into "buf" and null-terminates.
// Overwrites *np with the new length.
static const char* TerminateNumber(char* buf, int nbuf, const char* str, int* np,
                                   bool accept_spaces) {
  int n = *np;
  if (n <= 0) return "";
  if (n > 0 && isspace(*str)) {
    // We are less forgiving than the strtoxxx() routines and do not
    // allow leading spaces. We do allow leading spaces for floats.
    if (!accept_spaces) {
      return "";
    }
    while (n > 0 && isspace(*str)) {
      n--;
      str++;
    }
  }

  // Although buf has a fixed maximum size, we can still handle
  // arbitrarily large integers correctly by omitting leading zeros.
  // (Numbers that are still too long will be out of range.)
  // Before deciding whether str is too long,
  // remove leading zeros with s/000+/00/.
  // Leaving the leading two zeros in place means that
  // we don't change 0000x123 (invalid) into 0x123 (valid).
  // Skip over leading - before replacing.
  bool neg = false;
  if (n >= 1 && str[0] == '-') {
    neg = true;
    n--;
    str++;
  }

  if (n >= 3 && str[0] == '0' && str[1] == '0') {
    while (n >= 3 && str[2] == '0') {
      n--;
      str++;
    }
  }

  if (neg) {  // make room in buf for -
    n++;
    str--;
  }

  if (n > nbuf-1) return "";

  memmove(buf, str, n);
  if (neg) {
    buf[0] = '-';
  }
  buf[n] = '\0';
  *np = n;
  return buf;
}

bool RE2::Arg::parse_long_radix(const char* str,
                               int n,
                               void* dest,
                               int radix) {
  if (n == 0) return false;
  char buf[kMaxNumberLength+1];
  str = TerminateNumber(buf, sizeof buf, str, &n, false);
  char* end;
  errno = 0;
  long r = strtol(str, &end, radix);
  if (end != str + n) return false;   // Leftover junk
  if (errno) return false;
  if (dest == NULL) return true;
  *(reinterpret_cast<long*>(dest)) = r;
  return true;
}

bool RE2::Arg::parse_ulong_radix(const char* str,
                                int n,
                                void* dest,
                                int radix) {
  if (n == 0) return false;
  char buf[kMaxNumberLength+1];
  str = TerminateNumber(buf, sizeof buf, str, &n, false);
  if (str[0] == '-') {
   // strtoul() will silently accept negative numbers and parse
   // them.  This module is more strict and treats them as errors.
   return false;
  }

  char* end;
  errno = 0;
  unsigned long r = strtoul(str, &end, radix);
  if (end != str + n) return false;   // Leftover junk
  if (errno) return false;
  if (dest == NULL) return true;
  *(reinterpret_cast<unsigned long*>(dest)) = r;
  return true;
}

bool RE2::Arg::parse_short_radix(const char* str,
                                int n,
                                void* dest,
                                int radix) {
  long r;
  if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
  if ((short)r != r) return false;       // Out of range
  if (dest == NULL) return true;
  *(reinterpret_cast<short*>(dest)) = r;
  return true;
}

bool RE2::Arg::parse_ushort_radix(const char* str,
                                 int n,
                                 void* dest,
                                 int radix) {
  unsigned long r;
  if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
  if ((ushort)r != r) return false;                      // Out of range
  if (dest == NULL) return true;
  *(reinterpret_cast<unsigned short*>(dest)) = r;
  return true;
}

bool RE2::Arg::parse_int_radix(const char* str,
                              int n,
                              void* dest,
                              int radix) {
  long r;
  if (!parse_long_radix(str, n, &r, radix)) return false; // Could not parse
  if ((int)r != r) return false;         // Out of range
  if (dest == NULL) return true;
  *(reinterpret_cast<int*>(dest)) = r;
  return true;
}

bool RE2::Arg::parse_uint_radix(const char* str,
                               int n,
                               void* dest,
                               int radix) {
  unsigned long r;
  if (!parse_ulong_radix(str, n, &r, radix)) return false; // Could not parse
  if ((uint)r != r) return false;                       // Out of range
  if (dest == NULL) return true;
  *(reinterpret_cast<unsigned int*>(dest)) = r;
  return true;
}

#if RE2_HAVE_LONGLONG
bool RE2::Arg::parse_longlong_radix(const char* str,
                                   int n,
                                   void* dest,
                                   int radix) {
  if (n == 0) return false;
  char buf[kMaxNumberLength+1];
  str = TerminateNumber(buf, sizeof buf, str, &n, false);
  char* end;
  errno = 0;
  int64 r = strtoll(str, &end, radix);
  if (end != str + n) return false;   // Leftover junk
  if (errno) return false;
  if (dest == NULL) return true;
  *(reinterpret_cast<int64*>(dest)) = r;
  return true;
}

bool RE2::Arg::parse_ulonglong_radix(const char* str,
                                    int n,
                                    void* dest,
                                    int radix) {
  if (n == 0) return false;
  char buf[kMaxNumberLength+1];
  str = TerminateNumber(buf, sizeof buf, str, &n, false);
  if (str[0] == '-') {
    // strtoull() will silently accept negative numbers and parse
    // them.  This module is more strict and treats them as errors.
    return false;
  }
  char* end;
  errno = 0;
  uint64 r = strtoull(str, &end, radix);
  if (end != str + n) return false;   // Leftover junk
  if (errno) return false;
  if (dest == NULL) return true;
  *(reinterpret_cast<uint64*>(dest)) = r;
  return true;
}
#endif

static bool parse_double_float(const char* str, int n, bool isfloat, void *dest) {
  if (n == 0) return false;
  static const int kMaxLength = 200;
  char buf[kMaxLength+1];
  str = TerminateNumber(buf, sizeof buf, str, &n, true);
  char* end;
  errno = 0;
  double r;
  if (isfloat) {
    r = strtof(str, &end);
  } else {
    r = strtod(str, &end);
  }
  if (end != str + n) return false;   // Leftover junk
  if (errno) return false;
  if (dest == NULL) return true;
  if (isfloat) {
    *(reinterpret_cast<float*>(dest)) = r;
  } else {
    *(reinterpret_cast<double*>(dest)) = r;
  }
  return true;
}

bool RE2::Arg::parse_double(const char* str, int n, void* dest) {
  return parse_double_float(str, n, false, dest);
}

bool RE2::Arg::parse_float(const char* str, int n, void* dest) {
  return parse_double_float(str, n, true, dest);
}

#define DEFINE_INTEGER_PARSERS(name)                                        \
  bool RE2::Arg::parse_##name(const char* str, int n, void* dest) {          \
    return parse_##name##_radix(str, n, dest, 10);                          \
  }                                                                         \
  bool RE2::Arg::parse_##name##_hex(const char* str, int n, void* dest) {    \
    return parse_##name##_radix(str, n, dest, 16);                          \
  }                                                                         \
  bool RE2::Arg::parse_##name##_octal(const char* str, int n, void* dest) {  \
    return parse_##name##_radix(str, n, dest, 8);                           \
  }                                                                         \
  bool RE2::Arg::parse_##name##_cradix(const char* str, int n, void* dest) { \
    return parse_##name##_radix(str, n, dest, 0);                           \
  }

DEFINE_INTEGER_PARSERS(short);
DEFINE_INTEGER_PARSERS(ushort);
DEFINE_INTEGER_PARSERS(int);
DEFINE_INTEGER_PARSERS(uint);
DEFINE_INTEGER_PARSERS(long);
DEFINE_INTEGER_PARSERS(ulong);
DEFINE_INTEGER_PARSERS(longlong);
DEFINE_INTEGER_PARSERS(ulonglong);

#undef DEFINE_INTEGER_PARSERS

}  // namespace re2


text:
//$Id: ExtendsTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $
package org.hibernate.test.extendshbm;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.internal.StandardServiceRegistryImpl;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

/**
 * @author Gavin King
 */
public class ExtendsTest extends BaseUnitTestCase {
	private StandardServiceRegistryImpl serviceRegistry;

	@Before
	public void setUp() {
		serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry();
	}

	@After
	public void tearDown() {
		ServiceRegistryBuilder.destroy( serviceRegistry );
	}

	private String getBaseForMappings() {
		return "org/hibernate/test/";
	}

	@Test
	public void testAllInOne() {
		Configuration cfg = new Configuration();

		cfg.addResource( getBaseForMappings() + "extendshbm/allinone.hbm.xml" );
		cfg.buildMappings();
		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );
		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
		assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );
	}

	@Test
	public void testOutOfOrder() {
		Configuration cfg = new Configuration();

		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/Customer.hbm.xml" );
			assertNull(
					"cannot be in the configuration yet!",
					cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" )
			);
			cfg.addResource( getBaseForMappings() + "extendshbm/Person.hbm.xml" );
			cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" );

			cfg.buildSessionFactory( serviceRegistry );

			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );

		}
		catch ( HibernateException e ) {
			fail( "should not fail with exception! " + e );
		}

	}

	@Test
	public void testNwaitingForSuper() {
		Configuration cfg = new Configuration();

		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/Customer.hbm.xml" );
			assertNull(
					"cannot be in the configuration yet!",
					cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" )
			);
			cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" );
			assertNull(
					"cannot be in the configuration yet!",
					cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" )
			);
			cfg.addResource( getBaseForMappings() + "extendshbm/Person.hbm.xml" );

			cfg.buildMappings();

			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );

		}
		catch ( HibernateException e ) {
			e.printStackTrace();
			fail( "should not fail with exception! " + e );

		}

	}

	@Test
	public void testMissingSuper() {
		Configuration cfg = new Configuration();

		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/Customer.hbm.xml" );
			assertNull(
					"cannot be in the configuration yet!",
					cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" )
			);
			cfg.addResource( getBaseForMappings() + "extendshbm/Employee.hbm.xml" );

			cfg.buildSessionFactory( serviceRegistry );

			fail( "Should not be able to build sessionFactory without a Person" );
		}
		catch ( HibernateException e ) {

		}

	}

	@Test
	public void testAllSeparateInOne() {
		Configuration cfg = new Configuration();

		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/allseparateinone.hbm.xml" );

			cfg.buildSessionFactory( serviceRegistry );

			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Employee" ) );

		}
		catch ( HibernateException e ) {
			fail( "should not fail with exception! " + e );
		}

	}

	@Test
	public void testJoinedSubclassAndEntityNamesOnly() {
		Configuration cfg = new Configuration();

		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/entitynames.hbm.xml" );

			cfg.buildMappings();

			assertNotNull( cfg.getClassMapping( "EntityHasName" ) );
			assertNotNull( cfg.getClassMapping( "EntityCompany" ) );

		}
		catch ( HibernateException e ) {
			e.printStackTrace();
			fail( "should not fail with exception! " + e );

		}
	}

	@Test
	public void testEntityNamesWithPackage() {
		Configuration cfg = new Configuration();
		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/packageentitynames.hbm.xml" );

			cfg.buildMappings();

			assertNotNull( cfg.getClassMapping( "EntityHasName" ) );
			assertNotNull( cfg.getClassMapping( "EntityCompany" ) );

		}
		catch ( HibernateException e ) {
			e.printStackTrace();
			fail( "should not fail with exception! " + e );

		}
	}

	@Test
	public void testUnionSubclass() {
		Configuration cfg = new Configuration();

		try {
			cfg.addResource( getBaseForMappings() + "extendshbm/unionsubclass.hbm.xml" );

			cfg.buildMappings();

			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Person" ) );
			assertNotNull( cfg.getClassMapping( "org.hibernate.test.extendshbm.Customer" ) );

		}
		catch ( HibernateException e ) {
			e.printStackTrace();
			fail( "should not fail with exception! " + e );

		}
	}

}

redpajama_set_name: RedPajamaGithub


text:
import { mount } from "enzyme"
import React from "react"
import { act } from "react-dom/test-utils"
import {
  AuctionResultsFilterContextProvider,
  initialAuctionResultsFilterState,
  useAuctionResultsFilterContext,
} from "../AuctionResultsFilterContext"

describe("AuctionResultsFilterContext", () => {
  let context: ReturnType<typeof useAuctionResultsFilterContext>

  const getWrapper = (props = {}) => {
    return mount(
      <AuctionResultsFilterContextProvider {...props}>
        <TestComponent />
      </AuctionResultsFilterContextProvider>
    )
  }

  const TestComponent = () => {
    context = useAuctionResultsFilterContext()
    return null
  }

  it("boots with default filters", async () => {
    getWrapper()
    expect(context.filters).toEqual(initialAuctionResultsFilterState)
  })

  describe("behaviors", () => {
    // it("#onFilterClick", () => {
    //   const spy = jest.fn()
    //   getWrapper({ onFilterClick: spy })
    //   context.onFilterClick("color", "purple", initialAuctionResultsFilterState)
    //   expect(spy).toHaveBeenCalledWith(
    //     "color",
    //     "purple",
    //     initialAuctionResultsFilterState
    //   )
    // })

    it("#setFilter", done => {
      getWrapper()
      act(() => {
        context.setFilter("pageAndCursor", { page: 10, cursor: null })
        setTimeout(() => {
          // @ts-expect-error STRICT_NULL_CHECK
          expect(context.filters.pageAndCursor).toEqual({
            page: 10,
            cursor: null,
          })
          done()
        })
      })
    })

    it("#setFilter resets pagination", done => {
      getWrapper({
        filters: {
          pageAndCursor: { page: 10, cursor: null },
        },
      })
      act(() => {
        // @ts-expect-error STRICT_NULL_CHECK
        expect(context.filters.pageAndCursor.page).toEqual(10)
        context.setFilter("sort", "relevant")
        setTimeout(() => {
          // @ts-expect-error STRICT_NULL_CHECK
          expect(context.filters.pageAndCursor).toEqual({
            page: 1,
            cursor: null,
          })
          done()
        })
      })
    })

    describe("#setFilter for allowEmptyCreatedDates", () => {
      it("should set allowEmptyCreatedDates to false", done => {
        getWrapper({
          filters: {
            allowEmptyCreatedDates: true,
          },
        })
        act(() => {
          context.setFilter("allowEmptyCreatedDates", false)
          setTimeout(() => {
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.allowEmptyCreatedDates).toEqual(false)
            done()
          })
        })
      })
    })

    describe("#setFilter for createdAfterYear", () => {
      it("should set createdBeforeYear if it is not already provided", done => {
        getWrapper({
          filters: {
            latestCreatedYear: 2000,
          },
        })
        act(() => {
          context.setFilter("createdAfterYear", 1990)
          setTimeout(() => {
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdAfterYear).toEqual(1990)
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdBeforeYear).toEqual(2000)
            done()
          })
        })
      })

      it("should set createdBeforeYear to its value if createdBeforeYear is less", done => {
        getWrapper({
          filters: {
            createdBeforeYear: 2000,
          },
        })
        act(() => {
          context.setFilter("createdAfterYear", 2001)
          setTimeout(() => {
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdAfterYear).toEqual(2001)
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdBeforeYear).toEqual(2001)
            done()
          })
        })
      })
    })

    describe("#setFilter for createdBeforeYear", () => {
      it("should set createdAfterYear if it is not already provided", done => {
        getWrapper({
          filters: {
            earliestCreatedYear: 1990,
          },
        })
        act(() => {
          context.setFilter("createdBeforeYear", 2000)
          setTimeout(() => {
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdBeforeYear).toEqual(2000)
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdAfterYear).toEqual(1990)
            done()
          })
        })
      })

      it("should set createdAfterYear to its value if createdAfterYear is greater", done => {
        getWrapper({
          filters: {
            createdAfterYear: 2000,
          },
        })
        act(() => {
          context.setFilter("createdBeforeYear", 1990)
          setTimeout(() => {
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdBeforeYear).toEqual(1990)
            // @ts-expect-error STRICT_NULL_CHECK
            expect(context.filters.createdAfterYear).toEqual(1990)
            done()
          })
        })
      })
    })

    it("#unsetFilter", done => {
      getWrapper()
      act(() => {
        context.setFilter("pageAndCursor", { page: 10, cursor: null })
        setTimeout(() => {
          // @ts-expect-error STRICT_NULL_CHECK
          expect(context.filters.pageAndCursor.page).toEqual(10)
          act(() => {
            context.unsetFilter("pageAndCursor")
            setTimeout(() => {
              // @ts-expect-error STRICT_NULL_CHECK
              expect(context.filters.pageAndCursor).toEqual({
                page: 1,
                cursor: null,
              })
              done()
            })
          })
        })
      })
    })

    it("#unsetFilter resets pagination", done => {
      getWrapper({
        filters: {
          pageAndCursor: { page: 10, cursor: null },
          sort: "relevant",
        },
      })
      act(() => {
        // @ts-expect-error STRICT_NULL_CHECK
        expect(context.filters.pageAndCursor.page).toEqual(10)
        context.unsetFilter("sort")
        setTimeout(() => {
          // @ts-expect-error STRICT_NULL_CHECK
          expect(context.filters.pageAndCursor).toEqual({
            page: 1,
            cursor: null,
          })
          done()
        })
      })
    })

    it("#resetFilters", () => {
      getWrapper({
        filters: {
          ...initialAuctionResultsFilterState,
          organizations: [],
        },
      })
      // @ts-expect-error STRICT_NULL_CHECK
      expect(context.filters.organizations).toEqual([])

      act(() => {
        context.resetFilters()
        setTimeout(() => {
          expect(context.filters).toEqual(initialAuctionResultsFilterState)
        })
      })
    })
  })
})


text:
<?xml version="1.0" encoding="UTF-8" ?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity name="Task\TaskBundle\Entity\Task" table="ta_tasks"
            repository-class="Task\TaskBundle\Entity\TaskRepository">

        <indexes>
            <index columns="uuid"/>
        </indexes>

        <id name="uuid" type="guid">
            <generator strategy="NONE"/>
        </id>

        <field name="handlerClass" type="string" length="255"/>
        <field name="intervalExpression" type="string" length="255" nullable="true"/>
        <field name="firstExecution" type="datetime" nullable="true"/>
        <field name="lastExecution" type="datetime" nullable="true"/>
        <field name="systemKey" type="string" nullable="true" unique="true" length="191"/>
        <field name="workload" type="object"/>

    </entity>
</doctrine-mapping>


text:
package filesystem

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"time"

	"github.com/docker/distribution/context"
	storagedriver "github.com/docker/distribution/registry/storage/driver"
	"github.com/docker/distribution/registry/storage/driver/base"
	"github.com/docker/distribution/registry/storage/driver/factory"
)

const driverName = "filesystem"
const defaultRootDirectory = "/var/lib/registry"

func init() {
	factory.Register(driverName, &filesystemDriverFactory{})
}

// filesystemDriverFactory implements the factory.StorageDriverFactory interface
type filesystemDriverFactory struct{}

func (factory *filesystemDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
	return FromParameters(parameters), nil
}

type driver struct {
	rootDirectory string
}

type baseEmbed struct {
	base.Base
}

// Driver is a storagedriver.StorageDriver implementation backed by a local
// filesystem. All provided paths will be subpaths of the RootDirectory.
type Driver struct {
	baseEmbed
}

// FromParameters constructs a new Driver with a given parameters map
// Optional Parameters:
// - rootdirectory
func FromParameters(parameters map[string]interface{}) *Driver {
	var rootDirectory = defaultRootDirectory
	if parameters != nil {
		rootDir, ok := parameters["rootdirectory"]
		if ok {
			rootDirectory = fmt.Sprint(rootDir)
		}
	}
	return New(rootDirectory)
}

// New constructs a new Driver with a given rootDirectory
func New(rootDirectory string) *Driver {
	return &Driver{
		baseEmbed: baseEmbed{
			Base: base.Base{
				StorageDriver: &driver{
					rootDirectory: rootDirectory,
				},
			},
		},
	}
}

// Implement the storagedriver.StorageDriver interface

func (d *driver) Name() string {
	return driverName
}

// GetContent retrieves the content stored at "path" as a []byte.
func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) {
	rc, err := d.ReadStream(ctx, path, 0)
	if err != nil {
		return nil, err
	}
	defer rc.Close()

	p, err := ioutil.ReadAll(rc)
	if err != nil {
		return nil, err
	}

	return p, nil
}

// PutContent stores the []byte content at a location designated by "path".
func (d *driver) PutContent(ctx context.Context, subPath string, contents []byte) error {
	if _, err := d.WriteStream(ctx, subPath, 0, bytes.NewReader(contents)); err != nil {
		return err
	}

	// Fuse support: Disable truncate
	//return os.Truncate(d.fullPath(subPath), int64(len(contents)))
	// End
	return nil
}

// ReadStream retrieves an io.ReadCloser for the content stored at "path" with a
// given byte offset.
func (d *driver) ReadStream(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
	file, err := os.OpenFile(d.fullPath(path), os.O_RDONLY, 0644)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, storagedriver.PathNotFoundError{Path: path}
		}

		return nil, err
	}
	if offset == 0 {
		return file, nil
	}
	seekPos, err := file.Seek(int64(offset), os.SEEK_SET)
	//fmt.Printf("File Seek: %d, %v\n", seekPos, err)
	if err != nil {
		file.Close()
		return nil, err
	} else if seekPos < int64(offset) {
		file.Close()
		return nil, storagedriver.InvalidOffsetError{Path: path, Offset: offset}
	}

	return file, nil
}

// WriteStream stores the contents of the provided io.Reader at a location
// designated by the given path.
func (d *driver) WriteStream(ctx context.Context, subPath string, offset int64, reader io.Reader) (nn int64, err error) {
	// TODO(stevvooe): This needs to be a requirement.
	// if !path.IsAbs(subPath) {
	// 	return fmt.Errorf("absolute path required: %q", subPath)
	// }

	fullPath := d.fullPath(subPath)
	parentDir := path.Dir(fullPath)
	if err := os.MkdirAll(parentDir, 0755); err != nil {
		return 0, err
	}

	// Fuse support
	if offset == 0 {
		os.Remove(fullPath)
	}
	// End

	fp, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		// TODO(stevvooe): A few missing conditions in storage driver:
		//	1. What if the path is already a directory?
		//  2. Should number 1 be exposed explicitly in storagedriver?
		//	2. Can this path not exist, even if we create above?
		return 0, err
	}
	defer fp.Close()
	/*
		nn, err = fp.Seek(offset, os.SEEK_SET)
		if err != nil {
			return 0, err
		}

		if nn != offset {
			return 0, fmt.Errorf("bad seek to %v, expected %v in fp=%v", offset, nn, fp)
		}
	*/
	return io.Copy(fp, reader)
}

// Stat retrieves the FileInfo for the given path, including the current size
// in bytes and the creation time.
func (d *driver) Stat(ctx context.Context, subPath string) (storagedriver.FileInfo, error) {
	fullPath := d.fullPath(subPath)

	fi, err := os.Stat(fullPath)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, storagedriver.PathNotFoundError{Path: subPath}
		}

		return nil, err
	}

	return fileInfo{
		path:     subPath,
		FileInfo: fi,
	}, nil
}

// List returns a list of the objects that are direct descendants of the given
// path.
func (d *driver) List(ctx context.Context, subPath string) ([]string, error) {
	if subPath[len(subPath)-1] != '/' {
		subPath += "/"
	}
	fullPath := d.fullPath(subPath)

	dir, err := os.Open(fullPath)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, storagedriver.PathNotFoundError{Path: subPath}
		}
		return nil, err
	}

	defer dir.Close()

	fileNames, err := dir.Readdirnames(0)
	if err != nil {
		return nil, err
	}

	keys := make([]string, 0, len(fileNames))
	for _, fileName := range fileNames {
		keys = append(keys, path.Join(subPath, fileName))
	}

	return keys, nil
}

// Move moves an object stored at sourcePath to destPath, removing the original
// object.
func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) error {
	source := d.fullPath(sourcePath)
	dest := d.fullPath(destPath)

	if _, err := os.Stat(source); os.IsNotExist(err) {
		return storagedriver.PathNotFoundError{Path: sourcePath}
	}

	if err := os.MkdirAll(path.Dir(dest), 0755); err != nil {
		return err
	}

	err := os.Rename(source, dest)
	return err
}

// Delete recursively deletes all objects stored at "path" and its subpaths.
func (d *driver) Delete(ctx context.Context, subPath string) error {
	fullPath := d.fullPath(subPath)

	_, err := os.Stat(fullPath)
	if err != nil && !os.IsNotExist(err) {
		return err
	} else if err != nil {
		return storagedriver.PathNotFoundError{Path: subPath}
	}

	err = os.RemoveAll(fullPath)
	return err
}

// URLFor returns a URL which may be used to retrieve the content stored at the given path.
// May return an UnsupportedMethodErr in certain StorageDriver implementations.
func (d *driver) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
	return "", storagedriver.ErrUnsupportedMethod
}

// fullPath returns the absolute path of a key within the Driver's storage.
func (d *driver) fullPath(subPath string) string {
	return path.Join(d.rootDirectory, subPath)
}

type fileInfo struct {
	os.FileInfo
	path string
}

var _ storagedriver.FileInfo = fileInfo{}

// Path provides the full path of the target of this file info.
func (fi fileInfo) Path() string {
	return fi.path
}

// Size returns current length in bytes of the file. The return value can
// be used to write to the end of the file at path. The value is
// meaningless if IsDir returns true.
func (fi fileInfo) Size() int64 {
	if fi.IsDir() {
		return 0
	}

	return fi.FileInfo.Size()
}

// ModTime returns the modification time for the file. For backends that
// don't have a modification time, the creation time should be returned.
func (fi fileInfo) ModTime() time.Time {
	return fi.FileInfo.ModTime()
}

// IsDir returns true if the path is a directory.
func (fi fileInfo) IsDir() bool {
	return fi.FileInfo.IsDir()
}


text:
"""
Created on Jul 14, 2018

@author: eljeffe

    Copyright 2018 Root the Box

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""
# pylint: disable=unused-variable

from base64 import b64encode, b64decode
import codecs
import sys

def encode(s, name="utf-8", *args, **kwargs):
    if name == "base64":
        if isinstance(s, str):
            try:
                s = bytearray(s, "utf-8")
            except:
                pass
        return b64encode(s).decode("utf-8").strip()
    codec = codecs.lookup(name)
    rv, length = codec.encode(s, *args, **kwargs)
    if not isinstance(rv, (str, bytes, bytearray)):
        raise TypeError("Not a string or byte codec")
    return rv

def decode(s, name="utf-8", *args, **kwargs):
    if name == "base64":
        try:
            return bytearray(b64decode(s)).decode("utf-8")
        except:
            pass
    codec = codecs.lookup(name)
    rv, length = codec.decode(s, *args, **kwargs)
    if not isinstance(rv, (str, bytes, bytearray)):
        raise TypeError("Not a string or byte codec: type %s" % type(rv))
    return rv

def unicode(s, name="utf-8", *args, **kwargs):
    # used in python2 to return a unicode type from string
    codec = codecs.lookup(name)
    rv, length = codec.decode(s, *args, **kwargs)
    return rv

def set_type(value, basevalue):
    # try to set value to basevalue type
    basetype = type(basevalue)
    if type(value) == basetype:
        return value
    elif isinstance(basevalue, bool):
        if str(value).upper() == "TRUE":
            return True
        else:
            return False
    elif isinstance(basevalue, int):
        return int(value)
    elif type(unicode(value)) == basetype:
        return unicode(value)
    elif type(str(value)) == basetype:
        return str(value)
    elif type(bytes(value)) == basetype:
        return bytes(value)
    return value


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Organizations;
using NorthLion.Zero.OrganizationUnits.Dto;
using NorthLion.Zero.Users;

namespace NorthLion.Zero.OrganizationUnits
{
    public class OrganizationUnitsAppService : ZeroAppServiceBase, IOrganizationUnitsAppService
    {
        private readonly OrganizationUnitManager _organizationUnitManager;
        private readonly IRepository<OrganizationUnit, long> _organizationUnitRepository;
        private readonly IRepository<User, long> _usersRepository;

        public OrganizationUnitsAppService(OrganizationUnitManager organizationUnitManager, IRepository<OrganizationUnit, long> organizationUnitRepository, IRepository<User, long> usersRepository)
        {
            _organizationUnitManager = organizationUnitManager;
            _organizationUnitRepository = organizationUnitRepository;
            _usersRepository = usersRepository;
        }

        public async Task CreateOrganizationUnit(OrganizationUnitInput input)
        {
            await _organizationUnitManager.CreateAsync(new OrganizationUnit(AbpSession.TenantId, input.DisplayName,
                input.ParentId));
        }

        public async Task UpdateOrganizationUnit(UpdateOrganizationUnitInput input)
        {
            var orgUnit = _organizationUnitRepository.Get(input.Id);
            var updated = input.MapTo(orgUnit);
            await _organizationUnitManager.UpdateAsync(updated);
        }

        public async Task MoveOrganizationUnit(MoveOrganizationUnitInput input)
        {
            await _organizationUnitManager.MoveAsync(input.Id, input.ParentId);
        }

        public async Task AddUserToOrganizationUnit(long userId, int organizationUnitId)
        {
            var user = await UserManager.GetUserByIdAsync(userId);
            var organizationUnit = await _organizationUnitRepository.GetAsync(organizationUnitId);
            await UserManager.AddToOrganizationUnitAsync(user, organizationUnit);
        }

        public async Task<OrganizationUnitsTreeModel> GetOrganizationUnitsTree()
        {
            return new OrganizationUnitsTreeModel()
            {
               OrganizationUnits  = await GetOrganizationUnits()
            };
        }
        public UpdateOrganizationUnitInput GetOrganizationUnitForEdit(long id)
        {
            var orgUnit = _organizationUnitRepository.Get(id);
            return orgUnit.MapTo<UpdateOrganizationUnitInput>();
        }

        public async Task DeleteOrganizationUnit(long id)
        {
            await _organizationUnitManager.DeleteAsync(id);
        }

        public async Task DeleteUserFromOrganizationUnit(long userId, int organizationUnitId)
        {
            var user = await UserManager.GetUserByIdAsync(userId);
            var organizationUnit = await _organizationUnitRepository.GetAsync(organizationUnitId);
            await UserManager.RemoveFromOrganizationUnitAsync(user, organizationUnit);
        }

        #region Helpers

        private async Task<List<OrganizationUnitDto>> GetOrganizationUnits()
        {
            var listOrganizationUnits = new List<OrganizationUnitDto>();
            var parentUnits = (await _organizationUnitRepository.GetAllListAsync(a => a.ParentId == null));
            foreach (var organizationUnit in parentUnits)
            {
                var orgUnit = organizationUnit.MapTo<OrganizationUnitDto>();

                orgUnit.ChildrenDto = await GetChildren(organizationUnit);

                listOrganizationUnits.Add(orgUnit);

            }
            return listOrganizationUnits;
        }
        private async Task<List<OrganizationUnitDto>> GetChildren(OrganizationUnit organizationUnit)
        {
            var childs = new List<OrganizationUnitDto>();

            var children = await _organizationUnitManager.FindChildrenAsync(organizationUnit.Id);

            foreach (var child in children)
            {
                var childElement = child.MapTo<OrganizationUnitDto>();
                childElement.ChildrenDto = await GetChildren(child);

                childs.Add(childElement);
            }

            return childs;
        }
        #endregion
    }
}


text:
@interface RCTWebView (WebViewBridge)

@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;

- (void)injectBridgeScript:(NSNumber*)reactTag;
- (void)print;
- (void)eval:(NSString *) value;
- (void)bridgeSetup;
- (void)send:(NSString*)message;
- (void)callbackCleanup:(NSNumber *)reactTag;
- (void)onMessageCallback:(RCTResponseSenderBlock)callback withReactTag:(NSNumber *)reactTag;

//we are making this method visible to public. [Can't find any other way]
- (NSMutableDictionary *)baseEvent;

@end


text:
package com.benjaminabel.evawiki.model;

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class ArticleResponse {

    @SerializedName("items")
    private List<Article> articles;

    public List<Article> getArticles() {
        return articles;
    }

}


text:
package com.amazon.pay.response.ipn.model;

/**
 * Event types generated by the Amazon Pay
 * Billing Agreement IPNs
 */
public enum EventType {
    BillingAgreementStatusUpdated,
    PaymentMethodUpdated,
    AddressUpdated,
    AddressAndPaymentMethodUpdated
}


text:
CATEGORIES=	databases

MASTERDIR=	${.CURDIR}/../../lang/php7

PKGNAMESUFFIX=	-pgsql

.include "${MASTERDIR}/Makefile"


text:
#ifndef DiEAGL2Window_H__
#define DiEAGL2Window_H__

#include "ES2Prerequisites.h"
#include "Window.h"

#ifdef __OBJC__

#import "EAGL2View.h"
#import "EAGL2ViewController.h"

// Forward declarations
@class CAEAGLLayer;

// Define the native window type
typedef UIWindow *NativeWindowType;

#endif

namespace Demi
{
    class DiEAGL2Util;
    class DiEAGLES2Context;

    class DI_GLES2_API DiEAGL2Window : public DiWindow
    {
        protected:

            bool mIsFullScreen;

            bool mActive;

            bool mClosed;

            bool mVisible;

            uint32 mWidth;

            uint32 mHeight;

            /// Is this using an external window handle?
            bool mIsExternal;
            /// Is this using an external view handle?
            bool mUsingExternalView;
            /// Is this using an external view controller handle?
            bool mUsingExternalViewController;

            // iOS 4 content scaling
            bool mIsContentScalingSupported;

            float mContentScalingFactor;

            DiString mName;

            DiEAGL2Util* mGLSupport;

            DiEAGLES2Context* mContext{ nullptr };
#ifdef __OBJC__
            NativeWindowType mWindow{ nil };
            EAGL2View *mView{ nil };
            EAGL2ViewController *mViewController{ nil };
#endif

            void SwitchFullScreen(bool fullscreen) { }

            void GetLeftAndTopFromNativeWindow(int & left, int & top, uint width, uint height);

            void InitNativeCreatedWindow();

            void CreateNativeWindow(int &left, int &top, uint &width, uint &height, DiString &title);

            void Reposition(int left, int top);

	public:
            DiEAGL2Window(DiEAGL2Util* glsupport);

            virtual ~DiEAGL2Window();

            bool Create(uint32& width, uint32& height,
                                   const DiString& title, bool fullscreen);

			void SetFullscreen(bool fullscreen, uint width, uint height);

            DiEAGLES2Context* GetContext() {return mContext;}

            void Destroy(void);

            bool IsClosed(void) const { return mClosed; }

            bool IsVisible(void) const { return mVisible; }

            void SetVisible(bool visible) { mVisible = visible; }

            void SetClosed(bool closed) { mClosed = closed; }

            void SetWindowSize(uint32 width, uint32 height);

            void GetWindowSize(uint32& width, uint32& height);

            void SwapBuffers();

            void OnMoveOrResize();

            void Update();

            void Resize(uint32 width, uint32 height);

            void GetTitle(char *title, uint32 maxLength) const;

            void SetTitle(const char *title);

            bool Close();

            bool IsOpen();

            DiPixelFormat SuggestPixelFormat() const
            {
#if DEMI_ENDIAN == DEMI_BIG_ENDIAN
                return PF_R8G8B8A8; //TODO
#else
                return PF_A8B8G8R8;
#endif
            }
    };
}

#endif


text:
logo = """
  _________        .__  __    __
  \_   ___ \_______|__|/  |__/  |_  ___________________
  /    \  \/\_  __ \  \   __\   __\/ __ \_  __ \___   /
  \     \____|  | \/  ||  |  |  | \  ___/|  | \//    /
   \______  /|__|  |__||__|  |__|  \___  >__|  /_____ |
          \/                           \/            \/

 Input: text file with hash, ip, or domain on each line
 Output: JSON CRITS response, Fidelis Feed Format, and CRITS URLs
 Opens CRITS browser tabs unless -q is specified.  You must have a valid CRITS session logged into your browser.
 """

print logo

import json
import datetime
import optparse
import webbrowser
import time
import urllib
import urllib2
import os

try:
	import simplejson
except ImportError:
	print "Couldnt import simplejson. \n Try: 'sudo pip install simplejson'"
try:
	import requests
except ImportError:
	print "Couldnt import requests. \n Try: 'sudo pip install requests and sudo pip install requests[security]'"

dict = {}
vSleep = 1

#FUNCTIONS
def cDomain(i, src, buck):
	try:
		url = 'https://' + SERVER + '/api/v1/domains/'
		data = {
		'api_key': APIKEY,
		'username': USERNAME,
		'domain': i,
		'source': src,
		'bucket_list': buck,
		}
		r = requests.post(url, data=data, verify=False, timeout=10)
		time.sleep(vSleep)
		params = {
		'api_key': APIKEY,
		'username': USERNAME,
		'c-domain': i,
		}
		r = requests.get(url, params=params, verify=False, timeout=10)
		parsed = json.loads(r.content)
		print json.dumps(parsed, indent=4, sort_keys=True)
	except Exception as e:
		print 'Exception:', e.message

def cIP(i, src, buck):
	try:
		url = 'https://' + SERVER + '/api/v1/ips/'
		data = {
		'api_key': APIKEY,
		'username': USERNAME,
		'ip': i,
		'ip_type': "Address - ipv4-addr",
		'source': src,
		'bucket_list': buck,
		}
		r = requests.post(url, data=data, verify=False, timeout=10)
		time.sleep(vSleep)
		params = {
		'api_key': APIKEY,
		'username': USERNAME,
		'c-ip': i,
		}
		r = requests.get(url, params=params, verify=False, timeout=10)
		parsed = json.loads(r.content)
		print json.dumps(parsed, indent=4, sort_keys=True)
	except Exception as e:
		print 'Exception:', e.message

def cmd5(i, src, buck):
	try:
		url = 'https://' + SERVER + '/api/v1/samples/'
		data = {
		'api_key': APIKEY,
		'username': USERNAME,
		'md5': i,
		'upload_type': "metadata",
		'source': src,
		'bucket_list': buck,
		}
		r = requests.post(url, data=data, verify=False, timeout=10)
		time.sleep(vSleep)
		params = {
		'api_key': APIKEY,
		'username': USERNAME,
		'c-md5': i,
		}
		r = requests.get(url, params=params, verify=False, timeout=10)
		parsed = json.loads(r.content)
		print json.dumps(parsed, indent=4, sort_keys=True)
	except Exception as e:
		print 'Exception:', e.message

def cCampaign(camp, desc, buck):
	try:
		url = 'https://' + SERVER + '/api/v1/campaigns/'
		data = {
		'api_key': APIKEY,
		'username': USERNAME,
		'name': camp,
		'description': desc,
		'bucket_list': buck,
		}
		r = requests.post(url, data=data, verify=False, timeout=10)
		time.sleep(vSleep)
		params = {
		'api_key': APIKEY,
		'username': USERNAME,
		'c-campaign.name': camp,
		}
		r = requests.get(url, params=params, verify=False, timeout=10)
		parsed = json.loads(r.content)
		print json.dumps(parsed, indent=4, sort_keys=True)
	except Exception as e:
		print 'Exception:', e.message

def cRelationship(ltype, lid, rtype, rid, reltype):
	try:
		url = 'https://' + SERVER + '/api/v1/relationships/'
		data = {
		'api_key': APIKEY,
		'username': USERNAME,
		'left_type': ltype,
		'left_id': lid,
		'right_type': rtype,
		'right_id': rid,
		'rel_type': reltype,
		}
		print str(data)
		r = requests.post(url, data=data, verify=False, timeout=10)
		time.sleep(vSleep)
	except Exception as e:
		print 'Exception:', e.message

#MAIN
def main():
	parser = optparse.OptionParser('usage python critterz.py <-m mode> <-f filename> [-s intelSource] [-d feedDescription] [-b bucket] [ -q ] \n\n example: python critterz.py -m ip -f ip.txt -s TRT -d "Evil related" -b EvilMalware -c EvilCampaign')

	parser.add_option('-m', '--mode', dest='mode', type='string', help='required specify CRITs mode of either < domain | ip | hash >')
	parser.add_option('-f', '--filename', dest='filename', type='string', help='required specify filename')
	parser.add_option('-s', '--source', dest='source', type='string', help='optional specify source of intel (Defaults to TRT)')
	parser.add_option('-d', '--description', dest='description', type='string', help='optional specify feed description (Defaults to bucket entry or Unknown if bucket is empty)')
	parser.add_option('-b', '--bucket', dest='bucket', type='string', help='optional specify CRITs bucket (Defaults to Unknown)')
	parser.add_option("-q", action="store_true", dest="quiet", help="optional quiet mode will not open browswer tabs")
	#parser.add_option('-c', '--campaign', dest='campaign', type='string', help='optional specify CRITs campaign (Defaults to Unknown)')

	(options, args) = parser.parse_args()

	if (options.mode == None) | (options.filename == None):
		print parser.usage
		exit (0)

	cm = options.mode
	fn = options.filename

	if (options.source == None):
		src = "TRT"
	else:
		src = options.source

	if (options.description == None):
		if (options.bucket != None):
			desc = options.bucket
		else:
			desc = "Unknown"
	else:
		desc = options.description

	if (options.bucket == None):
		buck = "Unknown"
	else:
		buck = options.bucket

	if (options.quiet == True):
		quiet = "True"
	else:
		quiet = "False"

	requests.packages.urllib3.disable_warnings()
	fn = open(fn)
	dt = datetime.date.today().strftime('%m/%d/%Y')
	new = 2

	'''
	if (options.campaign == None):
		camp = "Unknown"
	else:
		camp = options.campaign
	if camp != "Unknown":
		cCampaign(camp,desc,buck)
	'''

	global SERVER
	global USERNAME
	global APIKEY

	if not os.path.exists("crits.conf"):
		cfn = open("crits.conf", "w+")
		SERVER = str(raw_input('Enter Server: ')) + "\n"
		USERNAME = str(raw_input('Enter Username: ')) + "\n"
		APIKEY = str(raw_input('Enter API Key: ')) + "\n"
		cfn.write(SERVER)
		cfn.write(USERNAME)
		cfn.write(APIKEY)
		cfn.close()

	cfn = open("crits.conf")
	SERVER = cfn.readline().rstrip("\n")
	USERNAME = cfn.readline().rstrip("\n")
	APIKEY = cfn.readline().rstrip("\n")
	cfn.close()

	if cm == "domain":
		a = []
		for i in fn:
	   		i = i.rstrip("\n")
			cDomain(i,src,buck)
			a.append(i)
			time.sleep(vSleep)

		print "\n *** Feed Syntax *** \n"
		for c in range(len(a)):
			print 'url,' + a[c] + ',' + desc + ',' + dt + ',,'

		print "\n *** CRITs URLs *** \n"
		for b in range(len(a)):
			url = 'https://' + SERVER + '/domains/details/'+ a[b] + '/#analysis_button'
			print url
			if quiet == "False":
				webbrowser.open(url,new=new)
				time.sleep(vSleep)

	elif cm == "ip":
		a = []
		for i in fn:
	   		i = i.rstrip("\n")
			cIP(i,src,buck)
			a.append(i)
			time.sleep(vSleep)

		print "\n *** Feed Syntax *** \n"
		for c in range(len(a)):
			print 'ip,' + a[c] + ',' + desc + ',' + dt + ',,'

		print "\n *** CRITs URLs *** \n"
		for b in range(len(a)):
			url = 'https://' + SERVER + '/ips/details/'+ a[b] + '/#analysis_button'
			print url
			if quiet == "False":
				webbrowser.open(url,new=new)
				time.sleep(vSleep)

	elif cm == "hash":
		a = []
		ftype = str(raw_input('Enter filetype [pe]: '))
		malname = str(raw_input('Enter malware name [Trojan.Win.EvilName.fss1]: '))
		maltype = str(raw_input('Enter malware type [Trojan]: '))
		alast = str(raw_input('Enter analyst last name [ProtectorOfTheRealm]: '))

		for i in fn:
	   		i = i.rstrip("\n")
			cmd5(i,src,buck)
			a.append(i)
			time.sleep(vSleep)

		print "\n *** Feed Syntax *** \n"
		for c in range(len(a)):
			print a[c] + ',,' + ftype + ',' + malname + ',' + maltype + ',4,' + desc + ',' + alast

		print "\n *** CRITs URLs *** \n"
		for b in range(len(a)):
			url = 'https://' + SERVER + '/samples/details/'+ a[b] + '/#analysis_button'
			print url
			if quiet == "False":
				webbrowser.open(url,new=new)
				time.sleep(vSleep)
	else:
		print parser.usage

if __name__ == '__main__':
	main()


text:
<!DOCTYPE html>

<link href="/pinot_static/css/lib/normalize.css" type="text/css" rel="stylesheet">
<link href="/pinot_static/css/lib/foundation.css" type="text/css" rel="stylesheet">
<link href="/pinot_static/css/lib/codemirror.css" type="text/css" rel="stylesheet">
<link href="/pinot_static/css/lib/codemirror-elegant.css" type="text/css" rel="stylesheet">

<link href="/pinot_static/css/pinot.css" type="text/css" rel="stylesheet">

<title>Pinot</title>

Pinot

        Fabrics

            {{fabric}}

Query Console

        Log Out ({{auth.username}})
        Log In

  <ng-view></ng-view>


text:
FROM biocontainers/biocontainers:vdebian-buster-backports_cv1
MAINTAINER biocontainers <biodocker@gmail.com>
LABEL    software="tnseq-transit" \
    base_image="biocontainers/biocontainers:vdebian-buster-backports_cv1" \
    container="tnseq-transit" \
    about.summary="statistical calculations of essentiality of genes or genomic regions" \
    about.home="http://saclab.tamu.edu/essentiality/transit/" \
    software.version="2.3.4-1-deb" \
    upstream.version="2.3.4" \
    version="1" \
    about.copyright="2015-2017 Michael A. DeJesus, Chaitra Ambadipudi, and  Thomas R. Ioerger" \
    about.license="GPL-3" \
    about.license_file="/usr/share/doc/tnseq-transit/copyright" \
    extra.binaries="/usr/bin/transit,/usr/bin/transit-tpp" \
    about.tags=""

USER root
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && (apt-get install -t buster-backports -y tnseq-transit || apt-get install -y tnseq-transit) && apt-get clean && apt-get purge && rm -rf /var/lib/apt/lists/* /tmp/*
USER biodocker


text:
Create an instance of the fluent interface by creating a new instance either via `new Utility($val)` or `Utility::make($val)`

You may pass an optional encoding type, otherwise it will default to the current internal character encoding.

``` php
$str = new Utility('Hello World');
echo $str;
// Hello World

$str = Utility::make('Foo Bar', 'UTF-8');
echo $str->value();
// Foo Bar
```

You can then chain  methods together to make life easy for yourself

```php
$str = new Utility('World');

echo $str->ensureBeingsWith('Hello ')->prepend('Foo Bar')->toSlug()->append('-123');
// hello-world-foo-bar-123
```


text: Located in the main RP repository https://github.com/reportportal/agent-net-nunit


text:
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache.servicecomb</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.0-m2-SNAPSHOT</version>
  </parent>
  <artifactId>spring-boot-starter-discovery</artifactId>
  <name>Java Chassis::Spring Boot Starter::Discovery</name>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <exclusions>
        <exclusion>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>log4j-over-slf4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-commons</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-context</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-netflix-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zuul</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
      <exclusions>
        <exclusion>
          <groupId>javax.ws.rs</groupId>
          <artifactId>jsr311-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.servicecomb</groupId>
      <artifactId>service-registry</artifactId>
    </dependency>
  </dependencies>
</project>


text:
extern bool isExistDataAt[13][32][8];
extern double scoreList[13][32][8];

//validate user's inputted date before inputting data
void validateDate(FILE *file) {
	printf(">> Add Data <<\n");

	int month, day;
	getDate(&month, &day);

	SET_DATE:;
	printf("Date is %d/%d.\n", month, day);
	printf("Is it correct? (Y if yes, any key for no) >> ");
	char reply;
	scanf("%c", &reply);
	if(reply == 'Y' || reply == 'y') {
		addData(file, month, day);
	} else {
		MANUAL_SET_DATE:;
		printf("Please input date.\n");
		printf("\tMonth >> "); scanf("%d", &month);
		printf("\t  Day >> "); scanf("%d", &day);
		clearInputBuffer();

		if(1 > month || 12 < month) {
			printf("Wrong month data inputted.\n");
			goto MANUAL_SET_DATE;
		}

		if(1 > day || 31 < day) {
			printf("Wrong day data inputted.\n");
			goto MANUAL_SET_DATE;
		} else {
			if(month == 2) {
				if(day > 29) {
					printf("Wrong day data inputted.\n");
					goto MANUAL_SET_DATE;
				}
			}
		}

		goto SET_DATE;
	}

}

//this adds data, and write to file
void addData(FILE *file, int month, int day) {
	printf(">> Adding data for %d/%d << \n", month, day);

	int classCount;
	SET_CLASS:;
	printf("What class will you input for? >> "); scanf("%d", &classCount);
	if(1 > classCount || 7 < classCount) {
		printf("Wrong class inputted.\n");
		goto SET_CLASS;
	}

	if(isExistDataAt[month][day][classCount]) {
		printf("Data already exists for %d/%d, %d%s class.\n", month, day, classCount, getOrdinalNumber(classCount));
		printf("Do you want to override it? (Y if yes, any key for no) >> ");
		clearInputBuffer();
		char reply;
		scanf("%c", &reply);
		if(!(reply == 'Y' || reply == 'y')) return;
	}
	GET_SCORE:;
	printf("Which point you got?\n");
	printf("1 for like, 0 for normal, -1 for dislike.\n");
	int score;
	printf(">> ");
	scanf("%d", &score);
	if(1 < score || -1 > score) {
		printf("Wrong point inputted.\n");
		goto GET_SCORE;
	}
	GET_NOSIT:;
	printf("How many students didn\'t sit down?\n");
	printf(">> ");
	int nosit;
	scanf("%d", &nosit);
	if(0 > nosit || 37 < nosit) {
		printf("Wrong didn\'t sit students count inputted.\n");
		goto GET_NOSIT;
	}

	double finalScore = score - (nosit * 0.5);
	printf("You\'ve got %.1lf degrees celcius at this time!\n", finalScore);
	fprintf(file, "%d %d %d %.1lf\n", month, day, classCount, finalScore);
	fflush(file);
	isExistDataAt[month][day][classCount] = true;
	scoreList[month][day][classCount] = finalScore;
}


text:
<?php

declare(strict_types=1);

namespace Laminas\Diactoros;

class Module
{
    public function getConfig(): array
    {
        return [
            'service_manager' => (new ConfigProvider())->getDependencies(),
        ];
    }
}


text:
#region License
//
// Copyright (c) 2013, Kooboo team
//
// Licensed under the BSD License
// See the file LICENSE.txt for details.
//
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.CMS.Search;
using Kooboo.CMS.Web.Areas.Contents.Models;
using Kooboo.CMS.Sites;
using Kooboo.CMS.Content.Models;
using Kooboo.Web.Mvc;
using Kooboo.CMS.Common;
using Kooboo.CMS.Search.Models;

namespace Kooboo.CMS.Web.Areas.Contents.Controllers
{
    public class SearchIndexController : ManagerControllerBase
    {
        IndexSummaryService IndexSummaryService { get; set; }
        public SearchIndexController(IndexSummaryService indexSummaryService)
        {
            IndexSummaryService = indexSummaryService;
        }
        #region Index
        public virtual ActionResult Index()
        {
            var viewModel = new IndexSummaryModel()
            {
                FolderIndexInfoes = IndexSummaryService.GetFolderIndexInfoes(Repository),
                LastActions = IndexSummaryService.GetLastActions(Repository)
            };
            return View(viewModel);
        }
        #endregion
        #region Rebuild
        public virtual ActionResult Rebuild(FolderIndexInfo[] model, string @return)
        {
            var data = new JsonResultData(ModelState);

            data.RunWithTry((resultData) =>
            {
                foreach (var item in model)
                {
                    var textFolder = new TextFolder(Repository, item.FolderName);
                    IndexSummaryService.Rebuild(textFolder);
                }
                data.RedirectUrl = @return;
            });

            return Json(data);
        }
        #endregion
    }
}


text:
/// <reference path="../../node_modules/phaser/typescript/phaser.d.ts"/>

/// <reference path="coordinates.ts"/>
/// <reference path="entities/gnome.ts"/>
/// <reference path="entities/tree.ts"/>
/// <reference path="world_constants.ts"/>
/// <reference path="victory_condition.ts"/>
/// <reference path="messages/message.ts"/>
///<reference path="entities/game_object.ts"/>
///<reference path="entities/house.ts"/>

/**
 * This class is responsible for keeping track of the world state and handling collisions.
 */
class GameWorld {
	constructor(public game: Phaser.Game) {
		this.blockGroup = game.add.group(game.world, "blocks");
		this.entityGroup = game.add.group(game.world, "entities");
	}

	public level: Level;

	private gnomes: Array<Gnome>;
	private gnomeCode: GnomeCode;
	private blockGroup: Phaser.Group;
	private entityGroup: Phaser.Group;

	private selectedBuilding: CodeBuilding;
	private selectedBuildingGnomeGhost: Gnome;

	public levelIsWon = false;
	public selectionListener: (building?: CodeBuilding, libraries?: CodeBuilding[]) => void;

	/**
	 * Loads the level with the provided name. It should be a JSON file that is loaded into the cache
	 * in the preloader.
	 */
	loadLevel(levelName: string) {
		let levelDefinition = this.game.cache.getJSON(levelName).LEVEL_DEFINITION;
		this.loadLevelFromDefinition(levelDefinition);
	}

	loadLevelFromDefinition(levelDefinition: any) {
		if (this.selectedBuildingGnomeGhost) {
			this.selectedBuildingGnomeGhost.destroy();
			this.selectedBuildingGnomeGhost = null;
		}
		this.selectedBuilding = null;
		this.levelIsWon = false;
		this.blockGroup.removeAll(true);
		this.entityGroup.removeAll(true);
		this.gnomes = [];
		if (this.selectionListener) {
			this.selectionListener();
		}

		this.level = new Level(levelDefinition);
		this.level.renderStage(this.blockGroup);
		this.level.renderObjects(this.entityGroup);
		if (levelDefinition.introMessage) {
			Messages.show(this.game, levelDefinition.introMessage);
		}
		this.gnomeCode = new GnomeCode(this.level.libraries.map(library => library.gnomeCode));

		this.level.codeBuildings.forEach(building => {
			building.events.onInputDown.add(() => this.selectCodeBuilding(building));
		});
		this.determineEntityZIndices();
	}

	selectCodeBuilding(building: CodeBuilding) {
		if (this.selectedBuilding) {
			this.selectedBuilding.deselect();
		}
		if (this.selectedBuildingGnomeGhost) {
			this.selectedBuildingGnomeGhost.destroy();
			this.selectedBuildingGnomeGhost = null;
		}
		this.selectedBuilding = building;
		this.selectedBuilding.select();
		if (this.selectionListener) {
			this.selectionListener(building, this.level.libraries);
		}
		if (building instanceof House) {
			this.selectedBuildingGnomeGhost = this.spawnGnome(building);
			this.selectedBuildingGnomeGhost.alpha = 0.3;
			this.game.add.tween(this.selectedBuildingGnomeGhost)
				.to({y: this.selectedBuildingGnomeGhost.y - 3}, 1000, Phaser.Easing.Sinusoidal.InOut, true)
				.yoyo(true, 0)
				.repeat(-1);
		}
	}

	/**
	 * Performs an action with the active gnome. Results depend on gnome location
	 */
	doGnomeAction(gnome: Gnome) {
		let actionLocation = gnome.location.getNeighbor(gnome.direction);
		let block = this.level.getBlock(actionLocation);

		let adjacentGnome = this.gnomes.filter(g => g.location.equals(actionLocation))[0];
		let adjacentObject = this.level.getObject(actionLocation);

		if (!adjacentObject || !adjacentObject.doAction(gnome)) {
			if (!gnome.floating && block === WorldConstants.BlockType.WATER) {
				gnome.wateringCan = true;
			}
			else if (adjacentGnome && gnome.wateringCan && !adjacentGnome.wateringCan) {
				gnome.wateringCan = false;
				adjacentGnome.wateringCan = true;
			}
			else {
				gnome.lookConfused();
			}
		}
	}

	/**
	 * Try to move the gnome forward. This will fail if there's an obstacle in the way.
	 */
	tryMove(gnome: Gnome): boolean {
		let newLocation = gnome.location.getNeighbor(gnome.direction);
		let gnomeExistsInLocation = this.gnomes.filter(g => g.location.equals(newLocation)).length;
		if (!gnomeExistsInLocation && this.level.pointIsPassable(newLocation)) {
			gnome.walkTo(newLocation);
			this.determineEntityZIndices();

			if (this.level.getBlock(newLocation) === WorldConstants.BlockType.SWAMP) {
				//Swamp slows gnomes down
				gnome.delayed++;
			}
			return true;
		}

		gnome.walkInPlace();
		return false;
	}

	killGnome(gnome: Gnome, causeOfDeath: CauseOfDeath) {
		this.gnomes.splice(this.gnomes.indexOf(gnome), 1);
		gnome.die(causeOfDeath);
	}

	resetGame() {
		this.gnomes.forEach((gnome) => {
			this.killGnome(gnome, CauseOfDeath.CODE_RAN_OUT);
		});
		this.level.softReset();
		ParticleEmitters.clearAll(this.game);
	}

	spawnGnomes() {
		if (this.selectedBuildingGnomeGhost) {
			this.selectedBuildingGnomeGhost.destroy();
			this.selectedBuildingGnomeGhost = null;
		}
		this.level.houses.forEach(house => {
			let newGnome = this.spawnGnome(house);
			this.entityGroup.add(newGnome);
			this.gnomes.push(newGnome);
			this.determineEntityZIndices();
		});
	}

	private spawnGnome(house): Gnome {
		return new Gnome(this.game,
			house.model.positionX + Direction.getXDelta(house.model.direction),
			house.model.positionY + Direction.getYDelta(house.model.direction),
			house.model.direction,
			this.addDelay(house.gnomeCode, house.delay));
	}

	hasLivingGnomes() {
		return this.gnomes.length > 0;
	}

	private addDelay(gnomeCode: Command[], delay: number) {
		let newGnomeCode = [];
		for (let i = 0; i < delay; i++) {
			newGnomeCode.push(new Command(CommandType.DELAY));
		}
		return newGnomeCode.concat(gnomeCode);
	}

	private determineEntityZIndices() {
		this.entityGroup.customSort((a, b) => {
				let aZIndex = a.location.x + a.location.y;
				let bZIndex = b.location.x + b.location.y;
				if (aZIndex > bZIndex) {
					return 1;
				}
				else if (aZIndex < bZIndex) {
					return -1;
				}
				return 0;
			});
	}

	/**
	 * Causes 1 unit of time to pass in the world.
	 */
	public nextTick() {
		this.level.spookTrees.forEach(tree => tree.digestFood());
		this.gnomeCode.executeNextCommand(this, this.gnomes);
		if (!this.levelIsWon && this.level.checkVictory()) {
			this.levelIsWon = true;
		}
	}
}