duckdb 0.3.5-dev974.0 → 0.3.5-dev992.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/duckdb.cpp CHANGED
@@ -1530,7 +1530,7 @@ public:
1530
1530
  }
1531
1531
 
1532
1532
  template <class T, class RETURN_TYPE = unique_ptr<T>, typename... ARGS>
1533
- RETURN_TYPE ReadSerializable(RETURN_TYPE default_value, ARGS &&... args) {
1533
+ RETURN_TYPE ReadSerializable(RETURN_TYPE default_value, ARGS &&...args) {
1534
1534
  if (field_count >= max_field_count) {
1535
1535
  // field is not there, read the default value
1536
1536
  return default_value;
@@ -1552,7 +1552,7 @@ public:
1552
1552
  }
1553
1553
 
1554
1554
  template <class T, class RETURN_TYPE = unique_ptr<T>, typename... ARGS>
1555
- RETURN_TYPE ReadRequiredSerializable(ARGS &&... args) {
1555
+ RETURN_TYPE ReadRequiredSerializable(ARGS &&...args) {
1556
1556
  if (field_count >= max_field_count) {
1557
1557
  // field is not there, read the default value
1558
1558
  throw SerializationException("Attempting to read mandatory field, but field is missing");
@@ -98696,6 +98696,9 @@ void AddFun::RegisterFunction(BuiltinFunctions &set) {
98696
98696
  functions.AddFunction(ListConcatFun::GetFunction());
98697
98697
 
98698
98698
  set.AddFunction(functions);
98699
+
98700
+ functions.name = "add";
98701
+ set.AddFunction(functions);
98699
98702
  }
98700
98703
 
98701
98704
  //===--------------------------------------------------------------------===//
@@ -98914,6 +98917,9 @@ void SubtractFun::RegisterFunction(BuiltinFunctions &set) {
98914
98917
  // we can negate intervals
98915
98918
  functions.AddFunction(GetFunction(LogicalType::INTERVAL));
98916
98919
  set.AddFunction(functions);
98920
+
98921
+ functions.name = "subtract";
98922
+ set.AddFunction(functions);
98917
98923
  }
98918
98924
 
98919
98925
  //===--------------------------------------------------------------------===//
@@ -99046,6 +99052,9 @@ void MultiplyFun::RegisterFunction(BuiltinFunctions &set) {
99046
99052
  ScalarFunction({LogicalType::BIGINT, LogicalType::INTERVAL}, LogicalType::INTERVAL,
99047
99053
  ScalarFunction::BinaryFunction<int64_t, interval_t, interval_t, MultiplyOperator>, true));
99048
99054
  set.AddFunction(functions);
99055
+
99056
+ functions.name = "multiply";
99057
+ set.AddFunction(functions);
99049
99058
  }
99050
99059
 
99051
99060
  //===--------------------------------------------------------------------===//
@@ -99167,6 +99176,9 @@ void DivideFun::RegisterFunction(BuiltinFunctions &set) {
99167
99176
  BinaryScalarFunctionIgnoreZero<interval_t, int64_t, interval_t, DivideOperator>));
99168
99177
 
99169
99178
  set.AddFunction(functions);
99179
+
99180
+ functions.name = "divide";
99181
+ set.AddFunction(functions);
99170
99182
  }
99171
99183
 
99172
99184
  //===--------------------------------------------------------------------===//
@@ -134738,6 +134750,34 @@ public:
134738
134750
 
134739
134751
  } // namespace duckdb
134740
134752
 
134753
+ //===----------------------------------------------------------------------===//
134754
+ // DuckDB
134755
+ //
134756
+ // duckdb/optimizer/rule/like_optimizations.hpp
134757
+ //
134758
+ //
134759
+ //===----------------------------------------------------------------------===//
134760
+
134761
+
134762
+
134763
+
134764
+
134765
+
134766
+ namespace duckdb {
134767
+
134768
+ class RegexOptimizationRule : public Rule {
134769
+ public:
134770
+ explicit RegexOptimizationRule(ExpressionRewriter &rewriter);
134771
+
134772
+ unique_ptr<Expression> Apply(LogicalOperator &op, vector<Expression *> &bindings, bool &changes_made,
134773
+ bool is_root) override;
134774
+
134775
+ unique_ptr<Expression> ApplyRule(BoundFunctionExpression *expr, ScalarFunction function, string pattern,
134776
+ bool is_not_like);
134777
+ };
134778
+
134779
+ } // namespace duckdb
134780
+
134741
134781
  //===----------------------------------------------------------------------===//
134742
134782
  // DuckDB
134743
134783
  //
@@ -134939,6 +134979,7 @@ Optimizer::Optimizer(Binder &binder, ClientContext &context) : context(context),
134939
134979
  rewriter.rules.push_back(make_unique<EqualOrNullSimplification>(rewriter));
134940
134980
  rewriter.rules.push_back(make_unique<MoveConstantsRule>(rewriter));
134941
134981
  rewriter.rules.push_back(make_unique<LikeOptimizationRule>(rewriter));
134982
+ rewriter.rules.push_back(make_unique<RegexOptimizationRule>(rewriter));
134942
134983
  rewriter.rules.push_back(make_unique<EmptyNeedleRemovalRule>(rewriter));
134943
134984
  rewriter.rules.push_back(make_unique<EnumComparisonRule>(rewriter));
134944
134985
 
@@ -137411,6 +137452,951 @@ unique_ptr<Expression> MoveConstantsRule::Apply(LogicalOperator &op, vector<Expr
137411
137452
 
137412
137453
 
137413
137454
 
137455
+
137456
+
137457
+
137458
+
137459
+
137460
+
137461
+ // LICENSE_CHANGE_BEGIN
137462
+ // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
137463
+ // See the end of this file for a list
137464
+
137465
+ // Copyright 2006 The RE2 Authors. All Rights Reserved.
137466
+ // Use of this source code is governed by a BSD-style
137467
+ // license that can be found in the LICENSE file.
137468
+
137469
+ #ifndef RE2_REGEXP_H_
137470
+ #define RE2_REGEXP_H_
137471
+
137472
+ // --- SPONSORED LINK --------------------------------------------------
137473
+ // If you want to use this library for regular expression matching,
137474
+ // you should use re2/re2.h, which provides a class RE2 that
137475
+ // mimics the PCRE interface provided by PCRE's C++ wrappers.
137476
+ // This header describes the low-level interface used to implement RE2
137477
+ // and may change in backwards-incompatible ways from time to time.
137478
+ // In contrast, RE2's interface will not.
137479
+ // ---------------------------------------------------------------------
137480
+
137481
+ // Regular expression library: parsing, execution, and manipulation
137482
+ // of regular expressions.
137483
+ //
137484
+ // Any operation that traverses the Regexp structures should be written
137485
+ // using Regexp::Walker (see walker-inl.h), not recursively, because deeply nested
137486
+ // regular expressions such as x++++++++++++++++++++... might cause recursive
137487
+ // traversals to overflow the stack.
137488
+ //
137489
+ // It is the caller's responsibility to provide appropriate mutual exclusion
137490
+ // around manipulation of the regexps. RE2 does this.
137491
+ //
137492
+ // PARSING
137493
+ //
137494
+ // Regexp::Parse parses regular expressions encoded in UTF-8.
137495
+ // The default syntax is POSIX extended regular expressions,
137496
+ // with the following changes:
137497
+ //
137498
+ // 1. Backreferences (optional in POSIX EREs) are not supported.
137499
+ // (Supporting them precludes the use of DFA-based
137500
+ // matching engines.)
137501
+ //
137502
+ // 2. Collating elements and collation classes are not supported.
137503
+ // (No one has needed or wanted them.)
137504
+ //
137505
+ // The exact syntax accepted can be modified by passing flags to
137506
+ // Regexp::Parse. In particular, many of the basic Perl additions
137507
+ // are available. The flags are documented below (search for LikePerl).
137508
+ //
137509
+ // If parsed with the flag Regexp::Latin1, both the regular expression
137510
+ // and the input to the matching routines are assumed to be encoded in
137511
+ // Latin-1, not UTF-8.
137512
+ //
137513
+ // EXECUTION
137514
+ //
137515
+ // Once Regexp has parsed a regular expression, it provides methods
137516
+ // to search text using that regular expression. These methods are
137517
+ // implemented via calling out to other regular expression libraries.
137518
+ // (Let's call them the sublibraries.)
137519
+ //
137520
+ // To call a sublibrary, Regexp does not simply prepare a
137521
+ // string version of the regular expression and hand it to the
137522
+ // sublibrary. Instead, Regexp prepares, from its own parsed form, the
137523
+ // corresponding internal representation used by the sublibrary.
137524
+ // This has the drawback of needing to know the internal representation
137525
+ // used by the sublibrary, but it has two important benefits:
137526
+ //
137527
+ // 1. The syntax and meaning of regular expressions is guaranteed
137528
+ // to be that used by Regexp's parser, not the syntax expected
137529
+ // by the sublibrary. Regexp might accept a restricted or
137530
+ // expanded syntax for regular expressions as compared with
137531
+ // the sublibrary. As long as Regexp can translate from its
137532
+ // internal form into the sublibrary's, clients need not know
137533
+ // exactly which sublibrary they are using.
137534
+ //
137535
+ // 2. The sublibrary parsers are bypassed. For whatever reason,
137536
+ // sublibrary regular expression parsers often have security
137537
+ // problems. For example, plan9grep's regular expression parser
137538
+ // has a buffer overflow in its handling of large character
137539
+ // classes, and PCRE's parser has had buffer overflow problems
137540
+ // in the past. Security-team requires sandboxing of sublibrary
137541
+ // regular expression parsers. Avoiding the sublibrary parsers
137542
+ // avoids the sandbox.
137543
+ //
137544
+ // The execution methods we use now are provided by the compiled form,
137545
+ // Prog, described in prog.h
137546
+ //
137547
+ // MANIPULATION
137548
+ //
137549
+ // Unlike other regular expression libraries, Regexp makes its parsed
137550
+ // form accessible to clients, so that client code can analyze the
137551
+ // parsed regular expressions.
137552
+
137553
+ #include <stdint.h>
137554
+ #include <map>
137555
+ #include <set>
137556
+ #include <string>
137557
+
137558
+
137559
+
137560
+ // LICENSE_CHANGE_BEGIN
137561
+ // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
137562
+ // See the end of this file for a list
137563
+
137564
+ // Copyright 2009 The RE2 Authors. All Rights Reserved.
137565
+ // Use of this source code is governed by a BSD-style
137566
+ // license that can be found in the LICENSE file.
137567
+
137568
+ #ifndef UTIL_UTIL_H_
137569
+ #define UTIL_UTIL_H_
137570
+
137571
+ #define arraysize(array) (int)(sizeof(array)/sizeof((array)[0]))
137572
+
137573
+ #ifndef ATTRIBUTE_NORETURN
137574
+ #if defined(__GNUC__)
137575
+ #define ATTRIBUTE_NORETURN __attribute__((noreturn))
137576
+ #elif defined(_MSC_VER)
137577
+ #define ATTRIBUTE_NORETURN __declspec(noreturn)
137578
+ #else
137579
+ #define ATTRIBUTE_NORETURN
137580
+ #endif
137581
+ #endif
137582
+
137583
+ #ifndef FALLTHROUGH_INTENDED
137584
+ #if defined(__clang__)
137585
+ #define FALLTHROUGH_INTENDED [[clang::fallthrough]]
137586
+ #elif defined(__GNUC__) && __GNUC__ >= 7
137587
+ #define FALLTHROUGH_INTENDED [[gnu::fallthrough]]
137588
+ #else
137589
+ #define FALLTHROUGH_INTENDED do {} while (0)
137590
+ #endif
137591
+ #endif
137592
+
137593
+ #ifndef NO_THREAD_SAFETY_ANALYSIS
137594
+ #define NO_THREAD_SAFETY_ANALYSIS
137595
+ #endif
137596
+
137597
+ #endif // UTIL_UTIL_H_
137598
+
137599
+
137600
+ // LICENSE_CHANGE_END
137601
+
137602
+
137603
+
137604
+ // LICENSE_CHANGE_BEGIN
137605
+ // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
137606
+ // See the end of this file for a list
137607
+
137608
+ // Copyright 2009 The RE2 Authors. All Rights Reserved.
137609
+ // Use of this source code is governed by a BSD-style
137610
+ // license that can be found in the LICENSE file.
137611
+
137612
+ #ifndef UTIL_LOGGING_H_
137613
+ #define UTIL_LOGGING_H_
137614
+
137615
+ // Simplified version of Google's logging.
137616
+
137617
+ #include <assert.h>
137618
+ #include <stdio.h>
137619
+ #include <stdlib.h>
137620
+ #include <ostream>
137621
+ #include <sstream>
137622
+
137623
+
137624
+
137625
+ // Debug-only checking.
137626
+ #define DCHECK(condition) assert(condition)
137627
+ #define DCHECK_EQ(val1, val2) assert((val1) == (val2))
137628
+ #define DCHECK_NE(val1, val2) assert((val1) != (val2))
137629
+ #define DCHECK_LE(val1, val2) assert((val1) <= (val2))
137630
+ #define DCHECK_LT(val1, val2) assert((val1) < (val2))
137631
+ #define DCHECK_GE(val1, val2) assert((val1) >= (val2))
137632
+ #define DCHECK_GT(val1, val2) assert((val1) > (val2))
137633
+
137634
+ // Always-on checking
137635
+ #define CHECK(x) if(x){}else LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
137636
+ #define CHECK_LT(x, y) CHECK((x) < (y))
137637
+ #define CHECK_GT(x, y) CHECK((x) > (y))
137638
+ #define CHECK_LE(x, y) CHECK((x) <= (y))
137639
+ #define CHECK_GE(x, y) CHECK((x) >= (y))
137640
+ #define CHECK_EQ(x, y) CHECK((x) == (y))
137641
+ #define CHECK_NE(x, y) CHECK((x) != (y))
137642
+
137643
+ #define LOG_INFO LogMessage(__FILE__, __LINE__)
137644
+ #define LOG_WARNING LogMessage(__FILE__, __LINE__)
137645
+ #define LOG_ERROR LogMessage(__FILE__, __LINE__)
137646
+ #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
137647
+ #define LOG_QFATAL LOG_FATAL
137648
+
137649
+ // It seems that one of the Windows header files defines ERROR as 0.
137650
+ #ifdef _WIN32
137651
+ #define LOG_0 LOG_INFO
137652
+ #endif
137653
+
137654
+ #ifdef NDEBUG
137655
+ #define LOG_DFATAL LOG_ERROR
137656
+ #else
137657
+ #define LOG_DFATAL LOG_FATAL
137658
+ #endif
137659
+
137660
+ #define LOG(severity) LOG_ ## severity.stream()
137661
+
137662
+ #define VLOG(x) if((x)>0){}else LOG_INFO.stream()
137663
+
137664
+ namespace duckdb_re2 {
137665
+
137666
+
137667
+ class LogMessage {
137668
+ public:
137669
+ LogMessage(const char* file, int line)
137670
+ : flushed_(false) {
137671
+ stream() << file << ":" << line << ": ";
137672
+ }
137673
+ void Flush() {
137674
+ stream() << "\n";
137675
+ /*// R does not allow us to have a reference to stderr even if we are not using it
137676
+ std::string s = str_.str();
137677
+ size_t n = s.size();
137678
+ if (fwrite(s.data(), 1, n, stderr) < n) {} // shut up gcc
137679
+ */
137680
+ flushed_ = true;
137681
+ }
137682
+ ~LogMessage() {
137683
+ if (!flushed_) {
137684
+ Flush();
137685
+ }
137686
+ }
137687
+ std::ostream& stream() { return str_; }
137688
+
137689
+ private:
137690
+ bool flushed_;
137691
+ std::ostringstream str_;
137692
+
137693
+ LogMessage(const LogMessage&) = delete;
137694
+ LogMessage& operator=(const LogMessage&) = delete;
137695
+ };
137696
+
137697
+ // Silence "destructor never returns" warning for ~LogMessageFatal().
137698
+ // Since this is a header file, push and then pop to limit the scope.
137699
+ #ifdef _MSC_VER
137700
+ //#pragma warning(push)
137701
+ //#pragma warning(disable: 4722)
137702
+ #endif
137703
+
137704
+ class LogMessageFatal : public LogMessage {
137705
+ public:
137706
+ LogMessageFatal(const char* file, int line)
137707
+ : LogMessage(file, line) {}
137708
+ ATTRIBUTE_NORETURN ~LogMessageFatal() {
137709
+ Flush();
137710
+ abort();
137711
+ }
137712
+ private:
137713
+ LogMessageFatal(const LogMessageFatal&) = delete;
137714
+ LogMessageFatal& operator=(const LogMessageFatal&) = delete;
137715
+ };
137716
+ } // namespace
137717
+
137718
+ #ifdef _MSC_VER
137719
+ //#pragma warning(pop)
137720
+ #endif
137721
+
137722
+ #endif // UTIL_LOGGING_H_
137723
+
137724
+
137725
+ // LICENSE_CHANGE_END
137726
+
137727
+
137728
+
137729
+ // LICENSE_CHANGE_BEGIN
137730
+ // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
137731
+ // See the end of this file for a list
137732
+
137733
+ /*
137734
+ * The authors of this software are Rob Pike and Ken Thompson.
137735
+ * Copyright (c) 2002 by Lucent Technologies.
137736
+ * Permission to use, copy, modify, and distribute this software for any
137737
+ * purpose without fee is hereby granted, provided that this entire notice
137738
+ * is included in all copies of any software which is or includes a copy
137739
+ * or modification of this software and in all copies of the supporting
137740
+ * documentation for such software.
137741
+ * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
137742
+ * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
137743
+ * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
137744
+ * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
137745
+ *
137746
+ * This file and rune.cc have been converted to compile as C++ code
137747
+ * in name space re2.
137748
+ */
137749
+
137750
+ #ifndef UTIL_UTF_H_
137751
+ #define UTIL_UTF_H_
137752
+
137753
+ #include <stdint.h>
137754
+
137755
+ namespace duckdb_re2 {
137756
+
137757
+ typedef signed int Rune; /* Code-point values in Unicode 4.0 are 21 bits wide.*/
137758
+
137759
+ enum
137760
+ {
137761
+ UTFmax = 4, /* maximum bytes per rune */
137762
+ Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
137763
+ Runeself = 0x80, /* rune and UTF sequences are the same (<) */
137764
+ Runeerror = 0xFFFD, /* decoding error in UTF */
137765
+ Runemax = 0x10FFFF, /* maximum rune value */
137766
+ };
137767
+
137768
+ int runetochar(char* s, const Rune* r);
137769
+ int chartorune(Rune* r, const char* s);
137770
+ int fullrune(const char* s, int n);
137771
+ int utflen(const char* s);
137772
+ char* utfrune(const char*, Rune);
137773
+
137774
+ } // namespace duckdb_re2
137775
+
137776
+ #endif // UTIL_UTF_H_
137777
+
137778
+
137779
+ // LICENSE_CHANGE_END
137780
+
137781
+
137782
+
137783
+ namespace duckdb_re2 {
137784
+
137785
+ // Keep in sync with string list kOpcodeNames[] in testing/dump.cc
137786
+ enum RegexpOp {
137787
+ // Matches no strings.
137788
+ kRegexpNoMatch = 1,
137789
+
137790
+ // Matches empty string.
137791
+ kRegexpEmptyMatch,
137792
+
137793
+ // Matches rune_.
137794
+ kRegexpLiteral,
137795
+
137796
+ // Matches runes_.
137797
+ kRegexpLiteralString,
137798
+
137799
+ // Matches concatenation of sub_[0..nsub-1].
137800
+ kRegexpConcat,
137801
+ // Matches union of sub_[0..nsub-1].
137802
+ kRegexpAlternate,
137803
+
137804
+ // Matches sub_[0] zero or more times.
137805
+ kRegexpStar,
137806
+ // Matches sub_[0] one or more times.
137807
+ kRegexpPlus,
137808
+ // Matches sub_[0] zero or one times.
137809
+ kRegexpQuest,
137810
+
137811
+ // Matches sub_[0] at least min_ times, at most max_ times.
137812
+ // max_ == -1 means no upper limit.
137813
+ kRegexpRepeat,
137814
+
137815
+ // Parenthesized (capturing) subexpression. Index is cap_.
137816
+ // Optionally, capturing name is name_.
137817
+ kRegexpCapture,
137818
+
137819
+ // Matches any character.
137820
+ kRegexpAnyChar,
137821
+
137822
+ // Matches any byte [sic].
137823
+ kRegexpAnyByte,
137824
+
137825
+ // Matches empty string at beginning of line.
137826
+ kRegexpBeginLine,
137827
+ // Matches empty string at end of line.
137828
+ kRegexpEndLine,
137829
+
137830
+ // Matches word boundary "\b".
137831
+ kRegexpWordBoundary,
137832
+ // Matches not-a-word boundary "\B".
137833
+ kRegexpNoWordBoundary,
137834
+
137835
+ // Matches empty string at beginning of text.
137836
+ kRegexpBeginText,
137837
+ // Matches empty string at end of text.
137838
+ kRegexpEndText,
137839
+
137840
+ // Matches character class given by cc_.
137841
+ kRegexpCharClass,
137842
+
137843
+ // Forces match of entire expression right now,
137844
+ // with match ID match_id_ (used by RE2::Set).
137845
+ kRegexpHaveMatch,
137846
+
137847
+ kMaxRegexpOp = kRegexpHaveMatch,
137848
+ };
137849
+
137850
+ // Keep in sync with string list in regexp.cc
137851
+ enum RegexpStatusCode {
137852
+ // No error
137853
+ kRegexpSuccess = 0,
137854
+
137855
+ // Unexpected error
137856
+ kRegexpInternalError,
137857
+
137858
+ // Parse errors
137859
+ kRegexpBadEscape, // bad escape sequence
137860
+ kRegexpBadCharClass, // bad character class
137861
+ kRegexpBadCharRange, // bad character class range
137862
+ kRegexpMissingBracket, // missing closing ]
137863
+ kRegexpMissingParen, // missing closing )
137864
+ kRegexpTrailingBackslash, // at end of regexp
137865
+ kRegexpRepeatArgument, // repeat argument missing, e.g. "*"
137866
+ kRegexpRepeatSize, // bad repetition argument
137867
+ kRegexpRepeatOp, // bad repetition operator
137868
+ kRegexpBadPerlOp, // bad perl operator
137869
+ kRegexpBadUTF8, // invalid UTF-8 in regexp
137870
+ kRegexpBadNamedCapture, // bad named capture
137871
+ };
137872
+
137873
+ // Error status for certain operations.
137874
+ class RegexpStatus {
137875
+ public:
137876
+ RegexpStatus() : code_(kRegexpSuccess), tmp_(NULL) {}
137877
+ ~RegexpStatus() { delete tmp_; }
137878
+
137879
+ void set_code(RegexpStatusCode code) { code_ = code; }
137880
+ void set_error_arg(const StringPiece& error_arg) { error_arg_ = error_arg; }
137881
+ void set_tmp(std::string* tmp) { delete tmp_; tmp_ = tmp; }
137882
+ RegexpStatusCode code() const { return code_; }
137883
+ const StringPiece& error_arg() const { return error_arg_; }
137884
+ bool ok() const { return code() == kRegexpSuccess; }
137885
+
137886
+ // Copies state from status.
137887
+ void Copy(const RegexpStatus& status);
137888
+
137889
+ // Returns text equivalent of code, e.g.:
137890
+ // "Bad character class"
137891
+ static std::string CodeText(RegexpStatusCode code);
137892
+
137893
+ // Returns text describing error, e.g.:
137894
+ // "Bad character class: [z-a]"
137895
+ std::string Text() const;
137896
+
137897
+ private:
137898
+ RegexpStatusCode code_; // Kind of error
137899
+ StringPiece error_arg_; // Piece of regexp containing syntax error.
137900
+ std::string* tmp_; // Temporary storage, possibly where error_arg_ is.
137901
+
137902
+ RegexpStatus(const RegexpStatus&) = delete;
137903
+ RegexpStatus& operator=(const RegexpStatus&) = delete;
137904
+ };
137905
+
137906
+ // Compiled form; see prog.h
137907
+ class Prog;
137908
+
137909
+ struct RuneRange {
137910
+ RuneRange() : lo(0), hi(0) { }
137911
+ RuneRange(int l, int h) : lo(l), hi(h) { }
137912
+ Rune lo;
137913
+ Rune hi;
137914
+ };
137915
+
137916
+ // Less-than on RuneRanges treats a == b if they overlap at all.
137917
+ // This lets us look in a set to find the range covering a particular Rune.
137918
+ struct RuneRangeLess {
137919
+ bool operator()(const RuneRange& a, const RuneRange& b) const {
137920
+ return a.hi < b.lo;
137921
+ }
137922
+ };
137923
+
137924
+ class CharClassBuilder;
137925
+
137926
+ class CharClass {
137927
+ public:
137928
+ void Delete();
137929
+
137930
+ typedef RuneRange* iterator;
137931
+ iterator begin() { return ranges_; }
137932
+ iterator end() { return ranges_ + nranges_; }
137933
+
137934
+ int size() { return nrunes_; }
137935
+ bool empty() { return nrunes_ == 0; }
137936
+ bool full() { return nrunes_ == Runemax+1; }
137937
+ bool FoldsASCII() { return folds_ascii_; }
137938
+
137939
+ bool Contains(Rune r);
137940
+ CharClass* Negate();
137941
+
137942
+ private:
137943
+ CharClass(); // not implemented
137944
+ ~CharClass(); // not implemented
137945
+ static CharClass* New(int maxranges);
137946
+
137947
+ friend class CharClassBuilder;
137948
+
137949
+ bool folds_ascii_;
137950
+ int nrunes_;
137951
+ RuneRange *ranges_;
137952
+ int nranges_;
137953
+
137954
+ CharClass(const CharClass&) = delete;
137955
+ CharClass& operator=(const CharClass&) = delete;
137956
+ };
137957
+
137958
+ struct repeat_t { // Repeat
137959
+ int max_;
137960
+ int min_;
137961
+ };
137962
+
137963
+ struct capture_t { // Capture
137964
+ int cap_;
137965
+ std::string* name_;
137966
+ };
137967
+
137968
+ struct literal_string_t{ // LiteralString
137969
+ int nrunes_;
137970
+ Rune* runes_;
137971
+ };
137972
+
137973
+ struct char_class_t { // CharClass
137974
+ // These two could be in separate union members,
137975
+ // but it wouldn't save any space (there are other two-word structs)
137976
+ // and keeping them separate avoids confusion during parsing.
137977
+ CharClass* cc_;
137978
+ CharClassBuilder* ccb_;
137979
+ };
137980
+
137981
+ class Regexp {
137982
+ public:
137983
+
137984
+ // Flags for parsing. Can be ORed together.
137985
+ enum ParseFlags {
137986
+ NoParseFlags = 0,
137987
+ FoldCase = 1<<0, // Fold case during matching (case-insensitive).
137988
+ Literal = 1<<1, // Treat s as literal string instead of a regexp.
137989
+ ClassNL = 1<<2, // Allow char classes like [^a-z] and \D and \s
137990
+ // and [[:space:]] to match newline.
137991
+ DotNL = 1<<3, // Allow . to match newline.
137992
+ MatchNL = ClassNL | DotNL,
137993
+ OneLine = 1<<4, // Treat ^ and $ as only matching at beginning and
137994
+ // end of text, not around embedded newlines.
137995
+ // (Perl's default)
137996
+ Latin1 = 1<<5, // Regexp and text are in Latin1, not UTF-8.
137997
+ NonGreedy = 1<<6, // Repetition operators are non-greedy by default.
137998
+ PerlClasses = 1<<7, // Allow Perl character classes like \d.
137999
+ PerlB = 1<<8, // Allow Perl's \b and \B.
138000
+ PerlX = 1<<9, // Perl extensions:
138001
+ // non-capturing parens - (?: )
138002
+ // non-greedy operators - *? +? ?? {}?
138003
+ // flag edits - (?i) (?-i) (?i: )
138004
+ // i - FoldCase
138005
+ // m - !OneLine
138006
+ // s - DotNL
138007
+ // U - NonGreedy
138008
+ // line ends: \A \z
138009
+ // \Q and \E to disable/enable metacharacters
138010
+ // (?P<name>expr) for named captures
138011
+ // \C to match any single byte
138012
+ UnicodeGroups = 1<<10, // Allow \p{Han} for Unicode Han group
138013
+ // and \P{Han} for its negation.
138014
+ NeverNL = 1<<11, // Never match NL, even if the regexp mentions
138015
+ // it explicitly.
138016
+ NeverCapture = 1<<12, // Parse all parens as non-capturing.
138017
+
138018
+ // As close to Perl as we can get.
138019
+ LikePerl = ClassNL | OneLine | PerlClasses | PerlB | PerlX |
138020
+ UnicodeGroups,
138021
+
138022
+ // Internal use only.
138023
+ WasDollar = 1<<13, // on kRegexpEndText: was $ in regexp text
138024
+ AllParseFlags = (1<<14)-1,
138025
+ };
138026
+
138027
+ // Get. No set, Regexps are logically immutable once created.
138028
+ RegexpOp op() { return static_cast<RegexpOp>(op_); }
138029
+ int nsub() { return nsub_; }
138030
+ bool simple() { return simple_ != 0; }
138031
+ ParseFlags parse_flags() { return static_cast<ParseFlags>(parse_flags_); }
138032
+ int Ref(); // For testing.
138033
+
138034
+ Regexp** sub() {
138035
+ if(nsub_ <= 1)
138036
+ return &subone_;
138037
+ else
138038
+ return submany_;
138039
+ }
138040
+
138041
+ int min() { DCHECK_EQ(op_, kRegexpRepeat); return repeat_.min_; }
138042
+ int max() { DCHECK_EQ(op_, kRegexpRepeat); return repeat_.max_; }
138043
+ Rune rune() { DCHECK_EQ(op_, kRegexpLiteral); return rune_; }
138044
+ CharClass* cc() { DCHECK_EQ(op_, kRegexpCharClass); return char_class_.cc_; }
138045
+ int cap() { DCHECK_EQ(op_, kRegexpCapture); return capture_.cap_; }
138046
+ const std::string* name() { DCHECK_EQ(op_, kRegexpCapture); return capture_.name_; }
138047
+ Rune* runes() { DCHECK_EQ(op_, kRegexpLiteralString); return literal_string_.runes_; }
138048
+ int nrunes() { DCHECK_EQ(op_, kRegexpLiteralString); return literal_string_.nrunes_; }
138049
+ int match_id() { DCHECK_EQ(op_, kRegexpHaveMatch); return match_id_; }
138050
+
138051
+ // Increments reference count, returns object as convenience.
138052
+ Regexp* Incref();
138053
+
138054
+ // Decrements reference count and deletes this object if count reaches 0.
138055
+ void Decref();
138056
+
138057
+ // Parses string s to produce regular expression, returned.
138058
+ // Caller must release return value with re->Decref().
138059
+ // On failure, sets *status (if status != NULL) and returns NULL.
138060
+ static Regexp* Parse(const StringPiece& s, ParseFlags flags,
138061
+ RegexpStatus* status);
138062
+
138063
+ // Returns a _new_ simplified version of the current regexp.
138064
+ // Does not edit the current regexp.
138065
+ // Caller must release return value with re->Decref().
138066
+ // Simplified means that counted repetition has been rewritten
138067
+ // into simpler terms and all Perl/POSIX features have been
138068
+ // removed. The result will capture exactly the same
138069
+ // subexpressions the original did, unless formatted with ToString.
138070
+ Regexp* Simplify();
138071
+ friend class CoalesceWalker;
138072
+ friend class SimplifyWalker;
138073
+
138074
+ // Parses the regexp src and then simplifies it and sets *dst to the
138075
+ // string representation of the simplified form. Returns true on success.
138076
+ // Returns false and sets *status (if status != NULL) on parse error.
138077
+ static bool SimplifyRegexp(const StringPiece& src, ParseFlags flags,
138078
+ std::string* dst, RegexpStatus* status);
138079
+
138080
+ // Returns the number of capturing groups in the regexp.
138081
+ int NumCaptures();
138082
+ friend class NumCapturesWalker;
138083
+
138084
+ // Returns a map from names to capturing group indices,
138085
+ // or NULL if the regexp contains no named capture groups.
138086
+ // The caller is responsible for deleting the map.
138087
+ std::map<std::string, int>* NamedCaptures();
138088
+
138089
+ // Returns a map from capturing group indices to capturing group
138090
+ // names or NULL if the regexp contains no named capture groups. The
138091
+ // caller is responsible for deleting the map.
138092
+ std::map<int, std::string>* CaptureNames();
138093
+
138094
+ // Returns a string representation of the current regexp,
138095
+ // using as few parentheses as possible.
138096
+ std::string ToString();
138097
+
138098
+ // Convenience functions. They consume the passed reference,
138099
+ // so in many cases you should use, e.g., Plus(re->Incref(), flags).
138100
+ // They do not consume allocated arrays like subs or runes.
138101
+ static Regexp* Plus(Regexp* sub, ParseFlags flags);
138102
+ static Regexp* Star(Regexp* sub, ParseFlags flags);
138103
+ static Regexp* Quest(Regexp* sub, ParseFlags flags);
138104
+ static Regexp* Concat(Regexp** subs, int nsubs, ParseFlags flags);
138105
+ static Regexp* Alternate(Regexp** subs, int nsubs, ParseFlags flags);
138106
+ static Regexp* Capture(Regexp* sub, ParseFlags flags, int cap);
138107
+ static Regexp* Repeat(Regexp* sub, ParseFlags flags, int min, int max);
138108
+ static Regexp* NewLiteral(Rune rune, ParseFlags flags);
138109
+ static Regexp* NewCharClass(CharClass* cc, ParseFlags flags);
138110
+ static Regexp* LiteralString(Rune* runes, int nrunes, ParseFlags flags);
138111
+ static Regexp* HaveMatch(int match_id, ParseFlags flags);
138112
+
138113
+ // Like Alternate but does not factor out common prefixes.
138114
+ static Regexp* AlternateNoFactor(Regexp** subs, int nsubs, ParseFlags flags);
138115
+
138116
+ // Debugging function. Returns string format for regexp
138117
+ // that makes structure clear. Does NOT use regexp syntax.
138118
+ std::string Dump();
138119
+
138120
+ // Helper traversal class, defined fully in walker-inl.h.
138121
+ template<typename T> class Walker;
138122
+
138123
+ // Compile to Prog. See prog.h
138124
+ // Reverse prog expects to be run over text backward.
138125
+ // Construction and execution of prog will
138126
+ // stay within approximately max_mem bytes of memory.
138127
+ // If max_mem <= 0, a reasonable default is used.
138128
+ Prog* CompileToProg(int64_t max_mem);
138129
+ Prog* CompileToReverseProg(int64_t max_mem);
138130
+
138131
+ // Whether to expect this library to find exactly the same answer as PCRE
138132
+ // when running this regexp. Most regexps do mimic PCRE exactly, but a few
138133
+ // obscure cases behave differently. Technically this is more a property
138134
+ // of the Prog than the Regexp, but the computation is much easier to do
138135
+ // on the Regexp. See mimics_pcre.cc for the exact conditions.
138136
+ bool MimicsPCRE();
138137
+
138138
+ // Benchmarking function.
138139
+ void NullWalk();
138140
+
138141
+ // Whether every match of this regexp must be anchored and
138142
+ // begin with a non-empty fixed string (perhaps after ASCII
138143
+ // case-folding). If so, returns the prefix and the sub-regexp that
138144
+ // follows it.
138145
+ // Callers should expect *prefix, *foldcase and *suffix to be "zeroed"
138146
+ // regardless of the return value.
138147
+ bool RequiredPrefix(std::string* prefix, bool* foldcase,
138148
+ Regexp** suffix);
138149
+
138150
+ private:
138151
+ // Constructor allocates vectors as appropriate for operator.
138152
+ explicit Regexp(RegexpOp op, ParseFlags parse_flags);
138153
+
138154
+ // Use Decref() instead of delete to release Regexps.
138155
+ // This is private to catch deletes at compile time.
138156
+ ~Regexp();
138157
+ void Destroy();
138158
+ bool QuickDestroy();
138159
+
138160
+ // Helpers for Parse. Listed here so they can edit Regexps.
138161
+ class ParseState;
138162
+
138163
+ friend class ParseState;
138164
+ friend bool ParseCharClass(StringPiece* s, Regexp** out_re,
138165
+ RegexpStatus* status);
138166
+
138167
+ // Helper for testing [sic].
138168
+ friend bool RegexpEqualTestingOnly(Regexp*, Regexp*);
138169
+
138170
+ // Computes whether Regexp is already simple.
138171
+ bool ComputeSimple();
138172
+
138173
+ // Constructor that generates a Star, Plus or Quest,
138174
+ // squashing the pair if sub is also a Star, Plus or Quest.
138175
+ static Regexp* StarPlusOrQuest(RegexpOp op, Regexp* sub, ParseFlags flags);
138176
+
138177
+ // Constructor that generates a concatenation or alternation,
138178
+ // enforcing the limit on the number of subexpressions for
138179
+ // a particular Regexp.
138180
+ static Regexp* ConcatOrAlternate(RegexpOp op, Regexp** subs, int nsubs,
138181
+ ParseFlags flags, bool can_factor);
138182
+
138183
+ // Returns the leading string that re starts with.
138184
+ // The returned Rune* points into a piece of re,
138185
+ // so it must not be used after the caller calls re->Decref().
138186
+ static Rune* LeadingString(Regexp* re, int* nrune, ParseFlags* flags);
138187
+
138188
+ // Removes the first n leading runes from the beginning of re.
138189
+ // Edits re in place.
138190
+ static void RemoveLeadingString(Regexp* re, int n);
138191
+
138192
+ // Returns the leading regexp in re's top-level concatenation.
138193
+ // The returned Regexp* points at re or a sub-expression of re,
138194
+ // so it must not be used after the caller calls re->Decref().
138195
+ static Regexp* LeadingRegexp(Regexp* re);
138196
+
138197
+ // Removes LeadingRegexp(re) from re and returns the remainder.
138198
+ // Might edit re in place.
138199
+ static Regexp* RemoveLeadingRegexp(Regexp* re);
138200
+
138201
+ // Simplifies an alternation of literal strings by factoring out
138202
+ // common prefixes.
138203
+ static int FactorAlternation(Regexp** sub, int nsub, ParseFlags flags);
138204
+ friend class FactorAlternationImpl;
138205
+
138206
+ // Is a == b? Only efficient on regexps that have not been through
138207
+ // Simplify yet - the expansion of a kRegexpRepeat will make this
138208
+ // take a long time. Do not call on such regexps, hence private.
138209
+ static bool Equal(Regexp* a, Regexp* b);
138210
+
138211
+ // Allocate space for n sub-regexps.
138212
+ void AllocSub(int n) {
138213
+ DCHECK(n >= 0 && static_cast<uint16_t>(n) == n);
138214
+ if (n > 1)
138215
+ submany_ = new Regexp*[n];
138216
+ nsub_ = static_cast<uint16_t>(n);
138217
+ }
138218
+
138219
+ // Add Rune to LiteralString
138220
+ void AddRuneToString(Rune r);
138221
+
138222
+ // Swaps this with that, in place.
138223
+ void Swap(Regexp *that);
138224
+
138225
+ // Operator. See description of operators above.
138226
+ // uint8_t instead of RegexpOp to control space usage.
138227
+ uint8_t op_;
138228
+
138229
+ // Is this regexp structure already simple
138230
+ // (has it been returned by Simplify)?
138231
+ // uint8_t instead of bool to control space usage.
138232
+ uint8_t simple_;
138233
+
138234
+ // Flags saved from parsing and used during execution.
138235
+ // (Only FoldCase is used.)
138236
+ // uint16_t instead of ParseFlags to control space usage.
138237
+ uint16_t parse_flags_;
138238
+
138239
+ // Reference count. Exists so that SimplifyRegexp can build
138240
+ // regexp structures that are dags rather than trees to avoid
138241
+ // exponential blowup in space requirements.
138242
+ // uint16_t to control space usage.
138243
+ // The standard regexp routines will never generate a
138244
+ // ref greater than the maximum repeat count (kMaxRepeat),
138245
+ // but even so, Incref and Decref consult an overflow map
138246
+ // when ref_ reaches kMaxRef.
138247
+ uint16_t ref_;
138248
+ static const uint16_t kMaxRef = 0xffff;
138249
+
138250
+ // Subexpressions.
138251
+ // uint16_t to control space usage.
138252
+ // Concat and Alternate handle larger numbers of subexpressions
138253
+ // by building concatenation or alternation trees.
138254
+ // Other routines should call Concat or Alternate instead of
138255
+ // filling in sub() by hand.
138256
+ uint16_t nsub_;
138257
+ static const uint16_t kMaxNsub = 0xffff;
138258
+ union {
138259
+ Regexp** submany_; // if nsub_ > 1
138260
+ Regexp* subone_; // if nsub_ == 1
138261
+ };
138262
+
138263
+ // Extra space for parse and teardown stacks.
138264
+ Regexp* down_;
138265
+
138266
+ // Arguments to operator. See description of operators above.
138267
+ union {
138268
+ repeat_t repeat_;
138269
+ capture_t capture_;
138270
+ literal_string_t literal_string_;
138271
+ char_class_t char_class_;
138272
+ Rune rune_; // Literal
138273
+ int match_id_; // HaveMatch
138274
+ void *the_union_[2]; // as big as any other element, for memset
138275
+ };
138276
+
138277
+ Regexp(const Regexp&) = delete;
138278
+ Regexp& operator=(const Regexp&) = delete;
138279
+ };
138280
+
138281
+ // Character class set: contains non-overlapping, non-abutting RuneRanges.
138282
+ typedef std::set<RuneRange, RuneRangeLess> RuneRangeSet;
138283
+
138284
+ class CharClassBuilder {
138285
+ public:
138286
+ CharClassBuilder();
138287
+
138288
+ typedef RuneRangeSet::iterator iterator;
138289
+ iterator begin() { return ranges_.begin(); }
138290
+ iterator end() { return ranges_.end(); }
138291
+
138292
+ int size() { return nrunes_; }
138293
+ bool empty() { return nrunes_ == 0; }
138294
+ bool full() { return nrunes_ == Runemax+1; }
138295
+
138296
+ bool Contains(Rune r);
138297
+ bool FoldsASCII();
138298
+ bool AddRange(Rune lo, Rune hi); // returns whether class changed
138299
+ CharClassBuilder* Copy();
138300
+ void AddCharClass(CharClassBuilder* cc);
138301
+ void Negate();
138302
+ void RemoveAbove(Rune r);
138303
+ CharClass* GetCharClass();
138304
+ void AddRangeFlags(Rune lo, Rune hi, Regexp::ParseFlags parse_flags);
138305
+
138306
+ private:
138307
+ static const uint32_t AlphaMask = (1<<26) - 1;
138308
+ uint32_t upper_; // bitmap of A-Z
138309
+ uint32_t lower_; // bitmap of a-z
138310
+ int nrunes_;
138311
+ RuneRangeSet ranges_;
138312
+
138313
+ CharClassBuilder(const CharClassBuilder&) = delete;
138314
+ CharClassBuilder& operator=(const CharClassBuilder&) = delete;
138315
+ };
138316
+
138317
+ // Bitwise ops on ParseFlags produce ParseFlags.
138318
+ inline Regexp::ParseFlags operator|(Regexp::ParseFlags a,
138319
+ Regexp::ParseFlags b) {
138320
+ return static_cast<Regexp::ParseFlags>(
138321
+ static_cast<int>(a) | static_cast<int>(b));
138322
+ }
138323
+
138324
+ inline Regexp::ParseFlags operator^(Regexp::ParseFlags a,
138325
+ Regexp::ParseFlags b) {
138326
+ return static_cast<Regexp::ParseFlags>(
138327
+ static_cast<int>(a) ^ static_cast<int>(b));
138328
+ }
138329
+
138330
+ inline Regexp::ParseFlags operator&(Regexp::ParseFlags a,
138331
+ Regexp::ParseFlags b) {
138332
+ return static_cast<Regexp::ParseFlags>(
138333
+ static_cast<int>(a) & static_cast<int>(b));
138334
+ }
138335
+
138336
+ inline Regexp::ParseFlags operator~(Regexp::ParseFlags a) {
138337
+ // Attempting to produce a value out of enum's range has undefined behaviour.
138338
+ return static_cast<Regexp::ParseFlags>(
138339
+ ~static_cast<int>(a) & static_cast<int>(Regexp::AllParseFlags));
138340
+ }
138341
+
138342
+ } // namespace duckdb_re2
138343
+
138344
+ #endif // RE2_REGEXP_H_
138345
+
138346
+
138347
+ // LICENSE_CHANGE_END
138348
+
138349
+
138350
+ namespace duckdb {
138351
+
138352
+ RegexOptimizationRule::RegexOptimizationRule(ExpressionRewriter &rewriter) : Rule(rewriter) {
138353
+ auto func = make_unique<FunctionExpressionMatcher>();
138354
+ func->function = make_unique<SpecificFunctionMatcher>("regexp_matches");
138355
+ func->policy = SetMatcher::Policy::ORDERED;
138356
+ func->matchers.push_back(make_unique<ExpressionMatcher>());
138357
+ func->matchers.push_back(make_unique<ConstantExpressionMatcher>());
138358
+ root = move(func);
138359
+ }
138360
+
138361
+ unique_ptr<Expression> RegexOptimizationRule::Apply(LogicalOperator &op, vector<Expression *> &bindings,
138362
+ bool &changes_made, bool is_root) {
138363
+ auto root = (BoundFunctionExpression *)bindings[0];
138364
+ auto constant_expr = (BoundConstantExpression *)bindings[2];
138365
+ D_ASSERT(root->children.size() == 2);
138366
+
138367
+ if (constant_expr->value.IsNull()) {
138368
+ return make_unique<BoundConstantExpression>(Value(root->return_type));
138369
+ }
138370
+
138371
+ // the constant_expr is a scalar expression that we have to fold
138372
+ if (!constant_expr->IsFoldable()) {
138373
+ return nullptr;
138374
+ }
138375
+
138376
+ auto constant_value = ExpressionExecutor::EvaluateScalar(*constant_expr);
138377
+ D_ASSERT(constant_value.type() == constant_expr->return_type);
138378
+ auto &patt_str = StringValue::Get(constant_value);
138379
+
138380
+ duckdb_re2::RE2 pattern(patt_str);
138381
+ if (!pattern.ok()) {
138382
+ return nullptr; // this should fail somewhere else
138383
+ }
138384
+
138385
+ if (pattern.Regexp()->op() == duckdb_re2::kRegexpLiteralString ||
138386
+ pattern.Regexp()->op() == duckdb_re2::kRegexpLiteral) {
138387
+ auto contains = make_unique<BoundFunctionExpression>(root->return_type, ContainsFun::GetFunction(),
138388
+ move(root->children), nullptr);
138389
+
138390
+ contains->children[1] = make_unique<BoundConstantExpression>(Value(patt_str));
138391
+ return move(contains);
138392
+ }
138393
+ return nullptr;
138394
+ }
138395
+
138396
+ } // namespace duckdb
138397
+
138398
+
138399
+
137414
138400
  namespace duckdb {
137415
138401
 
137416
138402
  unique_ptr<BaseStatistics> StatisticsPropagator::PropagateExpression(BoundAggregateExpression &aggr,
@@ -199024,173 +200010,6 @@ mz_bool mz_zip_end(mz_zip_archive *pZip)
199024
200010
 
199025
200011
 
199026
200012
 
199027
- // LICENSE_CHANGE_BEGIN
199028
- // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
199029
- // See the end of this file for a list
199030
-
199031
- // Copyright 2009 The RE2 Authors. All Rights Reserved.
199032
- // Use of this source code is governed by a BSD-style
199033
- // license that can be found in the LICENSE file.
199034
-
199035
- #ifndef UTIL_LOGGING_H_
199036
- #define UTIL_LOGGING_H_
199037
-
199038
- // Simplified version of Google's logging.
199039
-
199040
- #include <assert.h>
199041
- #include <stdio.h>
199042
- #include <stdlib.h>
199043
- #include <ostream>
199044
- #include <sstream>
199045
-
199046
-
199047
-
199048
- // LICENSE_CHANGE_BEGIN
199049
- // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
199050
- // See the end of this file for a list
199051
-
199052
- // Copyright 2009 The RE2 Authors. All Rights Reserved.
199053
- // Use of this source code is governed by a BSD-style
199054
- // license that can be found in the LICENSE file.
199055
-
199056
- #ifndef UTIL_UTIL_H_
199057
- #define UTIL_UTIL_H_
199058
-
199059
- #define arraysize(array) (int)(sizeof(array)/sizeof((array)[0]))
199060
-
199061
- #ifndef ATTRIBUTE_NORETURN
199062
- #if defined(__GNUC__)
199063
- #define ATTRIBUTE_NORETURN __attribute__((noreturn))
199064
- #elif defined(_MSC_VER)
199065
- #define ATTRIBUTE_NORETURN __declspec(noreturn)
199066
- #else
199067
- #define ATTRIBUTE_NORETURN
199068
- #endif
199069
- #endif
199070
-
199071
- #ifndef FALLTHROUGH_INTENDED
199072
- #if defined(__clang__)
199073
- #define FALLTHROUGH_INTENDED [[clang::fallthrough]]
199074
- #elif defined(__GNUC__) && __GNUC__ >= 7
199075
- #define FALLTHROUGH_INTENDED [[gnu::fallthrough]]
199076
- #else
199077
- #define FALLTHROUGH_INTENDED do {} while (0)
199078
- #endif
199079
- #endif
199080
-
199081
- #ifndef NO_THREAD_SAFETY_ANALYSIS
199082
- #define NO_THREAD_SAFETY_ANALYSIS
199083
- #endif
199084
-
199085
- #endif // UTIL_UTIL_H_
199086
-
199087
-
199088
- // LICENSE_CHANGE_END
199089
-
199090
-
199091
- // Debug-only checking.
199092
- #define DCHECK(condition) assert(condition)
199093
- #define DCHECK_EQ(val1, val2) assert((val1) == (val2))
199094
- #define DCHECK_NE(val1, val2) assert((val1) != (val2))
199095
- #define DCHECK_LE(val1, val2) assert((val1) <= (val2))
199096
- #define DCHECK_LT(val1, val2) assert((val1) < (val2))
199097
- #define DCHECK_GE(val1, val2) assert((val1) >= (val2))
199098
- #define DCHECK_GT(val1, val2) assert((val1) > (val2))
199099
-
199100
- // Always-on checking
199101
- #define CHECK(x) if(x){}else LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
199102
- #define CHECK_LT(x, y) CHECK((x) < (y))
199103
- #define CHECK_GT(x, y) CHECK((x) > (y))
199104
- #define CHECK_LE(x, y) CHECK((x) <= (y))
199105
- #define CHECK_GE(x, y) CHECK((x) >= (y))
199106
- #define CHECK_EQ(x, y) CHECK((x) == (y))
199107
- #define CHECK_NE(x, y) CHECK((x) != (y))
199108
-
199109
- #define LOG_INFO LogMessage(__FILE__, __LINE__)
199110
- #define LOG_WARNING LogMessage(__FILE__, __LINE__)
199111
- #define LOG_ERROR LogMessage(__FILE__, __LINE__)
199112
- #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
199113
- #define LOG_QFATAL LOG_FATAL
199114
-
199115
- // It seems that one of the Windows header files defines ERROR as 0.
199116
- #ifdef _WIN32
199117
- #define LOG_0 LOG_INFO
199118
- #endif
199119
-
199120
- #ifdef NDEBUG
199121
- #define LOG_DFATAL LOG_ERROR
199122
- #else
199123
- #define LOG_DFATAL LOG_FATAL
199124
- #endif
199125
-
199126
- #define LOG(severity) LOG_ ## severity.stream()
199127
-
199128
- #define VLOG(x) if((x)>0){}else LOG_INFO.stream()
199129
-
199130
- namespace duckdb_re2 {
199131
-
199132
-
199133
- class LogMessage {
199134
- public:
199135
- LogMessage(const char* file, int line)
199136
- : flushed_(false) {
199137
- stream() << file << ":" << line << ": ";
199138
- }
199139
- void Flush() {
199140
- stream() << "\n";
199141
- /*// R does not allow us to have a reference to stderr even if we are not using it
199142
- std::string s = str_.str();
199143
- size_t n = s.size();
199144
- if (fwrite(s.data(), 1, n, stderr) < n) {} // shut up gcc
199145
- */
199146
- flushed_ = true;
199147
- }
199148
- ~LogMessage() {
199149
- if (!flushed_) {
199150
- Flush();
199151
- }
199152
- }
199153
- std::ostream& stream() { return str_; }
199154
-
199155
- private:
199156
- bool flushed_;
199157
- std::ostringstream str_;
199158
-
199159
- LogMessage(const LogMessage&) = delete;
199160
- LogMessage& operator=(const LogMessage&) = delete;
199161
- };
199162
-
199163
- // Silence "destructor never returns" warning for ~LogMessageFatal().
199164
- // Since this is a header file, push and then pop to limit the scope.
199165
- #ifdef _MSC_VER
199166
- //#pragma warning(push)
199167
- //#pragma warning(disable: 4722)
199168
- #endif
199169
-
199170
- class LogMessageFatal : public LogMessage {
199171
- public:
199172
- LogMessageFatal(const char* file, int line)
199173
- : LogMessage(file, line) {}
199174
- ATTRIBUTE_NORETURN ~LogMessageFatal() {
199175
- Flush();
199176
- abort();
199177
- }
199178
- private:
199179
- LogMessageFatal(const LogMessageFatal&) = delete;
199180
- LogMessageFatal& operator=(const LogMessageFatal&) = delete;
199181
- };
199182
- } // namespace
199183
-
199184
- #ifdef _MSC_VER
199185
- //#pragma warning(pop)
199186
- #endif
199187
-
199188
- #endif // UTIL_LOGGING_H_
199189
-
199190
-
199191
- // LICENSE_CHANGE_END
199192
-
199193
-
199194
200013
 
199195
200014
  // LICENSE_CHANGE_BEGIN
199196
200015
  // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
@@ -200375,728 +201194,6 @@ class Prog {
200375
201194
 
200376
201195
 
200377
201196
 
200378
- // LICENSE_CHANGE_BEGIN
200379
- // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
200380
- // See the end of this file for a list
200381
-
200382
- // Copyright 2006 The RE2 Authors. All Rights Reserved.
200383
- // Use of this source code is governed by a BSD-style
200384
- // license that can be found in the LICENSE file.
200385
-
200386
- #ifndef RE2_REGEXP_H_
200387
- #define RE2_REGEXP_H_
200388
-
200389
- // --- SPONSORED LINK --------------------------------------------------
200390
- // If you want to use this library for regular expression matching,
200391
- // you should use re2/re2.h, which provides a class RE2 that
200392
- // mimics the PCRE interface provided by PCRE's C++ wrappers.
200393
- // This header describes the low-level interface used to implement RE2
200394
- // and may change in backwards-incompatible ways from time to time.
200395
- // In contrast, RE2's interface will not.
200396
- // ---------------------------------------------------------------------
200397
-
200398
- // Regular expression library: parsing, execution, and manipulation
200399
- // of regular expressions.
200400
- //
200401
- // Any operation that traverses the Regexp structures should be written
200402
- // using Regexp::Walker (see walker-inl.h), not recursively, because deeply nested
200403
- // regular expressions such as x++++++++++++++++++++... might cause recursive
200404
- // traversals to overflow the stack.
200405
- //
200406
- // It is the caller's responsibility to provide appropriate mutual exclusion
200407
- // around manipulation of the regexps. RE2 does this.
200408
- //
200409
- // PARSING
200410
- //
200411
- // Regexp::Parse parses regular expressions encoded in UTF-8.
200412
- // The default syntax is POSIX extended regular expressions,
200413
- // with the following changes:
200414
- //
200415
- // 1. Backreferences (optional in POSIX EREs) are not supported.
200416
- // (Supporting them precludes the use of DFA-based
200417
- // matching engines.)
200418
- //
200419
- // 2. Collating elements and collation classes are not supported.
200420
- // (No one has needed or wanted them.)
200421
- //
200422
- // The exact syntax accepted can be modified by passing flags to
200423
- // Regexp::Parse. In particular, many of the basic Perl additions
200424
- // are available. The flags are documented below (search for LikePerl).
200425
- //
200426
- // If parsed with the flag Regexp::Latin1, both the regular expression
200427
- // and the input to the matching routines are assumed to be encoded in
200428
- // Latin-1, not UTF-8.
200429
- //
200430
- // EXECUTION
200431
- //
200432
- // Once Regexp has parsed a regular expression, it provides methods
200433
- // to search text using that regular expression. These methods are
200434
- // implemented via calling out to other regular expression libraries.
200435
- // (Let's call them the sublibraries.)
200436
- //
200437
- // To call a sublibrary, Regexp does not simply prepare a
200438
- // string version of the regular expression and hand it to the
200439
- // sublibrary. Instead, Regexp prepares, from its own parsed form, the
200440
- // corresponding internal representation used by the sublibrary.
200441
- // This has the drawback of needing to know the internal representation
200442
- // used by the sublibrary, but it has two important benefits:
200443
- //
200444
- // 1. The syntax and meaning of regular expressions is guaranteed
200445
- // to be that used by Regexp's parser, not the syntax expected
200446
- // by the sublibrary. Regexp might accept a restricted or
200447
- // expanded syntax for regular expressions as compared with
200448
- // the sublibrary. As long as Regexp can translate from its
200449
- // internal form into the sublibrary's, clients need not know
200450
- // exactly which sublibrary they are using.
200451
- //
200452
- // 2. The sublibrary parsers are bypassed. For whatever reason,
200453
- // sublibrary regular expression parsers often have security
200454
- // problems. For example, plan9grep's regular expression parser
200455
- // has a buffer overflow in its handling of large character
200456
- // classes, and PCRE's parser has had buffer overflow problems
200457
- // in the past. Security-team requires sandboxing of sublibrary
200458
- // regular expression parsers. Avoiding the sublibrary parsers
200459
- // avoids the sandbox.
200460
- //
200461
- // The execution methods we use now are provided by the compiled form,
200462
- // Prog, described in prog.h
200463
- //
200464
- // MANIPULATION
200465
- //
200466
- // Unlike other regular expression libraries, Regexp makes its parsed
200467
- // form accessible to clients, so that client code can analyze the
200468
- // parsed regular expressions.
200469
-
200470
- #include <stdint.h>
200471
- #include <map>
200472
- #include <set>
200473
- #include <string>
200474
-
200475
-
200476
-
200477
-
200478
-
200479
- // LICENSE_CHANGE_BEGIN
200480
- // The following code up to LICENSE_CHANGE_END is subject to THIRD PARTY LICENSE #6
200481
- // See the end of this file for a list
200482
-
200483
- /*
200484
- * The authors of this software are Rob Pike and Ken Thompson.
200485
- * Copyright (c) 2002 by Lucent Technologies.
200486
- * Permission to use, copy, modify, and distribute this software for any
200487
- * purpose without fee is hereby granted, provided that this entire notice
200488
- * is included in all copies of any software which is or includes a copy
200489
- * or modification of this software and in all copies of the supporting
200490
- * documentation for such software.
200491
- * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
200492
- * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
200493
- * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
200494
- * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
200495
- *
200496
- * This file and rune.cc have been converted to compile as C++ code
200497
- * in name space re2.
200498
- */
200499
-
200500
- #ifndef UTIL_UTF_H_
200501
- #define UTIL_UTF_H_
200502
-
200503
- #include <stdint.h>
200504
-
200505
- namespace duckdb_re2 {
200506
-
200507
- typedef signed int Rune; /* Code-point values in Unicode 4.0 are 21 bits wide.*/
200508
-
200509
- enum
200510
- {
200511
- UTFmax = 4, /* maximum bytes per rune */
200512
- Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */
200513
- Runeself = 0x80, /* rune and UTF sequences are the same (<) */
200514
- Runeerror = 0xFFFD, /* decoding error in UTF */
200515
- Runemax = 0x10FFFF, /* maximum rune value */
200516
- };
200517
-
200518
- int runetochar(char* s, const Rune* r);
200519
- int chartorune(Rune* r, const char* s);
200520
- int fullrune(const char* s, int n);
200521
- int utflen(const char* s);
200522
- char* utfrune(const char*, Rune);
200523
-
200524
- } // namespace duckdb_re2
200525
-
200526
- #endif // UTIL_UTF_H_
200527
-
200528
-
200529
- // LICENSE_CHANGE_END
200530
-
200531
-
200532
-
200533
- namespace duckdb_re2 {
200534
-
200535
- // Keep in sync with string list kOpcodeNames[] in testing/dump.cc
200536
- enum RegexpOp {
200537
- // Matches no strings.
200538
- kRegexpNoMatch = 1,
200539
-
200540
- // Matches empty string.
200541
- kRegexpEmptyMatch,
200542
-
200543
- // Matches rune_.
200544
- kRegexpLiteral,
200545
-
200546
- // Matches runes_.
200547
- kRegexpLiteralString,
200548
-
200549
- // Matches concatenation of sub_[0..nsub-1].
200550
- kRegexpConcat,
200551
- // Matches union of sub_[0..nsub-1].
200552
- kRegexpAlternate,
200553
-
200554
- // Matches sub_[0] zero or more times.
200555
- kRegexpStar,
200556
- // Matches sub_[0] one or more times.
200557
- kRegexpPlus,
200558
- // Matches sub_[0] zero or one times.
200559
- kRegexpQuest,
200560
-
200561
- // Matches sub_[0] at least min_ times, at most max_ times.
200562
- // max_ == -1 means no upper limit.
200563
- kRegexpRepeat,
200564
-
200565
- // Parenthesized (capturing) subexpression. Index is cap_.
200566
- // Optionally, capturing name is name_.
200567
- kRegexpCapture,
200568
-
200569
- // Matches any character.
200570
- kRegexpAnyChar,
200571
-
200572
- // Matches any byte [sic].
200573
- kRegexpAnyByte,
200574
-
200575
- // Matches empty string at beginning of line.
200576
- kRegexpBeginLine,
200577
- // Matches empty string at end of line.
200578
- kRegexpEndLine,
200579
-
200580
- // Matches word boundary "\b".
200581
- kRegexpWordBoundary,
200582
- // Matches not-a-word boundary "\B".
200583
- kRegexpNoWordBoundary,
200584
-
200585
- // Matches empty string at beginning of text.
200586
- kRegexpBeginText,
200587
- // Matches empty string at end of text.
200588
- kRegexpEndText,
200589
-
200590
- // Matches character class given by cc_.
200591
- kRegexpCharClass,
200592
-
200593
- // Forces match of entire expression right now,
200594
- // with match ID match_id_ (used by RE2::Set).
200595
- kRegexpHaveMatch,
200596
-
200597
- kMaxRegexpOp = kRegexpHaveMatch,
200598
- };
200599
-
200600
- // Keep in sync with string list in regexp.cc
200601
- enum RegexpStatusCode {
200602
- // No error
200603
- kRegexpSuccess = 0,
200604
-
200605
- // Unexpected error
200606
- kRegexpInternalError,
200607
-
200608
- // Parse errors
200609
- kRegexpBadEscape, // bad escape sequence
200610
- kRegexpBadCharClass, // bad character class
200611
- kRegexpBadCharRange, // bad character class range
200612
- kRegexpMissingBracket, // missing closing ]
200613
- kRegexpMissingParen, // missing closing )
200614
- kRegexpTrailingBackslash, // at end of regexp
200615
- kRegexpRepeatArgument, // repeat argument missing, e.g. "*"
200616
- kRegexpRepeatSize, // bad repetition argument
200617
- kRegexpRepeatOp, // bad repetition operator
200618
- kRegexpBadPerlOp, // bad perl operator
200619
- kRegexpBadUTF8, // invalid UTF-8 in regexp
200620
- kRegexpBadNamedCapture, // bad named capture
200621
- };
200622
-
200623
- // Error status for certain operations.
200624
- class RegexpStatus {
200625
- public:
200626
- RegexpStatus() : code_(kRegexpSuccess), tmp_(NULL) {}
200627
- ~RegexpStatus() { delete tmp_; }
200628
-
200629
- void set_code(RegexpStatusCode code) { code_ = code; }
200630
- void set_error_arg(const StringPiece& error_arg) { error_arg_ = error_arg; }
200631
- void set_tmp(std::string* tmp) { delete tmp_; tmp_ = tmp; }
200632
- RegexpStatusCode code() const { return code_; }
200633
- const StringPiece& error_arg() const { return error_arg_; }
200634
- bool ok() const { return code() == kRegexpSuccess; }
200635
-
200636
- // Copies state from status.
200637
- void Copy(const RegexpStatus& status);
200638
-
200639
- // Returns text equivalent of code, e.g.:
200640
- // "Bad character class"
200641
- static std::string CodeText(RegexpStatusCode code);
200642
-
200643
- // Returns text describing error, e.g.:
200644
- // "Bad character class: [z-a]"
200645
- std::string Text() const;
200646
-
200647
- private:
200648
- RegexpStatusCode code_; // Kind of error
200649
- StringPiece error_arg_; // Piece of regexp containing syntax error.
200650
- std::string* tmp_; // Temporary storage, possibly where error_arg_ is.
200651
-
200652
- RegexpStatus(const RegexpStatus&) = delete;
200653
- RegexpStatus& operator=(const RegexpStatus&) = delete;
200654
- };
200655
-
200656
- // Compiled form; see prog.h
200657
- class Prog;
200658
-
200659
- struct RuneRange {
200660
- RuneRange() : lo(0), hi(0) { }
200661
- RuneRange(int l, int h) : lo(l), hi(h) { }
200662
- Rune lo;
200663
- Rune hi;
200664
- };
200665
-
200666
- // Less-than on RuneRanges treats a == b if they overlap at all.
200667
- // This lets us look in a set to find the range covering a particular Rune.
200668
- struct RuneRangeLess {
200669
- bool operator()(const RuneRange& a, const RuneRange& b) const {
200670
- return a.hi < b.lo;
200671
- }
200672
- };
200673
-
200674
- class CharClassBuilder;
200675
-
200676
- class CharClass {
200677
- public:
200678
- void Delete();
200679
-
200680
- typedef RuneRange* iterator;
200681
- iterator begin() { return ranges_; }
200682
- iterator end() { return ranges_ + nranges_; }
200683
-
200684
- int size() { return nrunes_; }
200685
- bool empty() { return nrunes_ == 0; }
200686
- bool full() { return nrunes_ == Runemax+1; }
200687
- bool FoldsASCII() { return folds_ascii_; }
200688
-
200689
- bool Contains(Rune r);
200690
- CharClass* Negate();
200691
-
200692
- private:
200693
- CharClass(); // not implemented
200694
- ~CharClass(); // not implemented
200695
- static CharClass* New(int maxranges);
200696
-
200697
- friend class CharClassBuilder;
200698
-
200699
- bool folds_ascii_;
200700
- int nrunes_;
200701
- RuneRange *ranges_;
200702
- int nranges_;
200703
-
200704
- CharClass(const CharClass&) = delete;
200705
- CharClass& operator=(const CharClass&) = delete;
200706
- };
200707
-
200708
- struct repeat_t { // Repeat
200709
- int max_;
200710
- int min_;
200711
- };
200712
-
200713
- struct capture_t { // Capture
200714
- int cap_;
200715
- std::string* name_;
200716
- };
200717
-
200718
- struct literal_string_t{ // LiteralString
200719
- int nrunes_;
200720
- Rune* runes_;
200721
- };
200722
-
200723
- struct char_class_t { // CharClass
200724
- // These two could be in separate union members,
200725
- // but it wouldn't save any space (there are other two-word structs)
200726
- // and keeping them separate avoids confusion during parsing.
200727
- CharClass* cc_;
200728
- CharClassBuilder* ccb_;
200729
- };
200730
-
200731
- class Regexp {
200732
- public:
200733
-
200734
- // Flags for parsing. Can be ORed together.
200735
- enum ParseFlags {
200736
- NoParseFlags = 0,
200737
- FoldCase = 1<<0, // Fold case during matching (case-insensitive).
200738
- Literal = 1<<1, // Treat s as literal string instead of a regexp.
200739
- ClassNL = 1<<2, // Allow char classes like [^a-z] and \D and \s
200740
- // and [[:space:]] to match newline.
200741
- DotNL = 1<<3, // Allow . to match newline.
200742
- MatchNL = ClassNL | DotNL,
200743
- OneLine = 1<<4, // Treat ^ and $ as only matching at beginning and
200744
- // end of text, not around embedded newlines.
200745
- // (Perl's default)
200746
- Latin1 = 1<<5, // Regexp and text are in Latin1, not UTF-8.
200747
- NonGreedy = 1<<6, // Repetition operators are non-greedy by default.
200748
- PerlClasses = 1<<7, // Allow Perl character classes like \d.
200749
- PerlB = 1<<8, // Allow Perl's \b and \B.
200750
- PerlX = 1<<9, // Perl extensions:
200751
- // non-capturing parens - (?: )
200752
- // non-greedy operators - *? +? ?? {}?
200753
- // flag edits - (?i) (?-i) (?i: )
200754
- // i - FoldCase
200755
- // m - !OneLine
200756
- // s - DotNL
200757
- // U - NonGreedy
200758
- // line ends: \A \z
200759
- // \Q and \E to disable/enable metacharacters
200760
- // (?P<name>expr) for named captures
200761
- // \C to match any single byte
200762
- UnicodeGroups = 1<<10, // Allow \p{Han} for Unicode Han group
200763
- // and \P{Han} for its negation.
200764
- NeverNL = 1<<11, // Never match NL, even if the regexp mentions
200765
- // it explicitly.
200766
- NeverCapture = 1<<12, // Parse all parens as non-capturing.
200767
-
200768
- // As close to Perl as we can get.
200769
- LikePerl = ClassNL | OneLine | PerlClasses | PerlB | PerlX |
200770
- UnicodeGroups,
200771
-
200772
- // Internal use only.
200773
- WasDollar = 1<<13, // on kRegexpEndText: was $ in regexp text
200774
- AllParseFlags = (1<<14)-1,
200775
- };
200776
-
200777
- // Get. No set, Regexps are logically immutable once created.
200778
- RegexpOp op() { return static_cast<RegexpOp>(op_); }
200779
- int nsub() { return nsub_; }
200780
- bool simple() { return simple_ != 0; }
200781
- ParseFlags parse_flags() { return static_cast<ParseFlags>(parse_flags_); }
200782
- int Ref(); // For testing.
200783
-
200784
- Regexp** sub() {
200785
- if(nsub_ <= 1)
200786
- return &subone_;
200787
- else
200788
- return submany_;
200789
- }
200790
-
200791
- int min() { DCHECK_EQ(op_, kRegexpRepeat); return repeat_.min_; }
200792
- int max() { DCHECK_EQ(op_, kRegexpRepeat); return repeat_.max_; }
200793
- Rune rune() { DCHECK_EQ(op_, kRegexpLiteral); return rune_; }
200794
- CharClass* cc() { DCHECK_EQ(op_, kRegexpCharClass); return char_class_.cc_; }
200795
- int cap() { DCHECK_EQ(op_, kRegexpCapture); return capture_.cap_; }
200796
- const std::string* name() { DCHECK_EQ(op_, kRegexpCapture); return capture_.name_; }
200797
- Rune* runes() { DCHECK_EQ(op_, kRegexpLiteralString); return literal_string_.runes_; }
200798
- int nrunes() { DCHECK_EQ(op_, kRegexpLiteralString); return literal_string_.nrunes_; }
200799
- int match_id() { DCHECK_EQ(op_, kRegexpHaveMatch); return match_id_; }
200800
-
200801
- // Increments reference count, returns object as convenience.
200802
- Regexp* Incref();
200803
-
200804
- // Decrements reference count and deletes this object if count reaches 0.
200805
- void Decref();
200806
-
200807
- // Parses string s to produce regular expression, returned.
200808
- // Caller must release return value with re->Decref().
200809
- // On failure, sets *status (if status != NULL) and returns NULL.
200810
- static Regexp* Parse(const StringPiece& s, ParseFlags flags,
200811
- RegexpStatus* status);
200812
-
200813
- // Returns a _new_ simplified version of the current regexp.
200814
- // Does not edit the current regexp.
200815
- // Caller must release return value with re->Decref().
200816
- // Simplified means that counted repetition has been rewritten
200817
- // into simpler terms and all Perl/POSIX features have been
200818
- // removed. The result will capture exactly the same
200819
- // subexpressions the original did, unless formatted with ToString.
200820
- Regexp* Simplify();
200821
- friend class CoalesceWalker;
200822
- friend class SimplifyWalker;
200823
-
200824
- // Parses the regexp src and then simplifies it and sets *dst to the
200825
- // string representation of the simplified form. Returns true on success.
200826
- // Returns false and sets *status (if status != NULL) on parse error.
200827
- static bool SimplifyRegexp(const StringPiece& src, ParseFlags flags,
200828
- std::string* dst, RegexpStatus* status);
200829
-
200830
- // Returns the number of capturing groups in the regexp.
200831
- int NumCaptures();
200832
- friend class NumCapturesWalker;
200833
-
200834
- // Returns a map from names to capturing group indices,
200835
- // or NULL if the regexp contains no named capture groups.
200836
- // The caller is responsible for deleting the map.
200837
- std::map<std::string, int>* NamedCaptures();
200838
-
200839
- // Returns a map from capturing group indices to capturing group
200840
- // names or NULL if the regexp contains no named capture groups. The
200841
- // caller is responsible for deleting the map.
200842
- std::map<int, std::string>* CaptureNames();
200843
-
200844
- // Returns a string representation of the current regexp,
200845
- // using as few parentheses as possible.
200846
- std::string ToString();
200847
-
200848
- // Convenience functions. They consume the passed reference,
200849
- // so in many cases you should use, e.g., Plus(re->Incref(), flags).
200850
- // They do not consume allocated arrays like subs or runes.
200851
- static Regexp* Plus(Regexp* sub, ParseFlags flags);
200852
- static Regexp* Star(Regexp* sub, ParseFlags flags);
200853
- static Regexp* Quest(Regexp* sub, ParseFlags flags);
200854
- static Regexp* Concat(Regexp** subs, int nsubs, ParseFlags flags);
200855
- static Regexp* Alternate(Regexp** subs, int nsubs, ParseFlags flags);
200856
- static Regexp* Capture(Regexp* sub, ParseFlags flags, int cap);
200857
- static Regexp* Repeat(Regexp* sub, ParseFlags flags, int min, int max);
200858
- static Regexp* NewLiteral(Rune rune, ParseFlags flags);
200859
- static Regexp* NewCharClass(CharClass* cc, ParseFlags flags);
200860
- static Regexp* LiteralString(Rune* runes, int nrunes, ParseFlags flags);
200861
- static Regexp* HaveMatch(int match_id, ParseFlags flags);
200862
-
200863
- // Like Alternate but does not factor out common prefixes.
200864
- static Regexp* AlternateNoFactor(Regexp** subs, int nsubs, ParseFlags flags);
200865
-
200866
- // Debugging function. Returns string format for regexp
200867
- // that makes structure clear. Does NOT use regexp syntax.
200868
- std::string Dump();
200869
-
200870
- // Helper traversal class, defined fully in walker-inl.h.
200871
- template<typename T> class Walker;
200872
-
200873
- // Compile to Prog. See prog.h
200874
- // Reverse prog expects to be run over text backward.
200875
- // Construction and execution of prog will
200876
- // stay within approximately max_mem bytes of memory.
200877
- // If max_mem <= 0, a reasonable default is used.
200878
- Prog* CompileToProg(int64_t max_mem);
200879
- Prog* CompileToReverseProg(int64_t max_mem);
200880
-
200881
- // Whether to expect this library to find exactly the same answer as PCRE
200882
- // when running this regexp. Most regexps do mimic PCRE exactly, but a few
200883
- // obscure cases behave differently. Technically this is more a property
200884
- // of the Prog than the Regexp, but the computation is much easier to do
200885
- // on the Regexp. See mimics_pcre.cc for the exact conditions.
200886
- bool MimicsPCRE();
200887
-
200888
- // Benchmarking function.
200889
- void NullWalk();
200890
-
200891
- // Whether every match of this regexp must be anchored and
200892
- // begin with a non-empty fixed string (perhaps after ASCII
200893
- // case-folding). If so, returns the prefix and the sub-regexp that
200894
- // follows it.
200895
- // Callers should expect *prefix, *foldcase and *suffix to be "zeroed"
200896
- // regardless of the return value.
200897
- bool RequiredPrefix(std::string* prefix, bool* foldcase,
200898
- Regexp** suffix);
200899
-
200900
- private:
200901
- // Constructor allocates vectors as appropriate for operator.
200902
- explicit Regexp(RegexpOp op, ParseFlags parse_flags);
200903
-
200904
- // Use Decref() instead of delete to release Regexps.
200905
- // This is private to catch deletes at compile time.
200906
- ~Regexp();
200907
- void Destroy();
200908
- bool QuickDestroy();
200909
-
200910
- // Helpers for Parse. Listed here so they can edit Regexps.
200911
- class ParseState;
200912
-
200913
- friend class ParseState;
200914
- friend bool ParseCharClass(StringPiece* s, Regexp** out_re,
200915
- RegexpStatus* status);
200916
-
200917
- // Helper for testing [sic].
200918
- friend bool RegexpEqualTestingOnly(Regexp*, Regexp*);
200919
-
200920
- // Computes whether Regexp is already simple.
200921
- bool ComputeSimple();
200922
-
200923
- // Constructor that generates a Star, Plus or Quest,
200924
- // squashing the pair if sub is also a Star, Plus or Quest.
200925
- static Regexp* StarPlusOrQuest(RegexpOp op, Regexp* sub, ParseFlags flags);
200926
-
200927
- // Constructor that generates a concatenation or alternation,
200928
- // enforcing the limit on the number of subexpressions for
200929
- // a particular Regexp.
200930
- static Regexp* ConcatOrAlternate(RegexpOp op, Regexp** subs, int nsubs,
200931
- ParseFlags flags, bool can_factor);
200932
-
200933
- // Returns the leading string that re starts with.
200934
- // The returned Rune* points into a piece of re,
200935
- // so it must not be used after the caller calls re->Decref().
200936
- static Rune* LeadingString(Regexp* re, int* nrune, ParseFlags* flags);
200937
-
200938
- // Removes the first n leading runes from the beginning of re.
200939
- // Edits re in place.
200940
- static void RemoveLeadingString(Regexp* re, int n);
200941
-
200942
- // Returns the leading regexp in re's top-level concatenation.
200943
- // The returned Regexp* points at re or a sub-expression of re,
200944
- // so it must not be used after the caller calls re->Decref().
200945
- static Regexp* LeadingRegexp(Regexp* re);
200946
-
200947
- // Removes LeadingRegexp(re) from re and returns the remainder.
200948
- // Might edit re in place.
200949
- static Regexp* RemoveLeadingRegexp(Regexp* re);
200950
-
200951
- // Simplifies an alternation of literal strings by factoring out
200952
- // common prefixes.
200953
- static int FactorAlternation(Regexp** sub, int nsub, ParseFlags flags);
200954
- friend class FactorAlternationImpl;
200955
-
200956
- // Is a == b? Only efficient on regexps that have not been through
200957
- // Simplify yet - the expansion of a kRegexpRepeat will make this
200958
- // take a long time. Do not call on such regexps, hence private.
200959
- static bool Equal(Regexp* a, Regexp* b);
200960
-
200961
- // Allocate space for n sub-regexps.
200962
- void AllocSub(int n) {
200963
- DCHECK(n >= 0 && static_cast<uint16_t>(n) == n);
200964
- if (n > 1)
200965
- submany_ = new Regexp*[n];
200966
- nsub_ = static_cast<uint16_t>(n);
200967
- }
200968
-
200969
- // Add Rune to LiteralString
200970
- void AddRuneToString(Rune r);
200971
-
200972
- // Swaps this with that, in place.
200973
- void Swap(Regexp *that);
200974
-
200975
- // Operator. See description of operators above.
200976
- // uint8_t instead of RegexpOp to control space usage.
200977
- uint8_t op_;
200978
-
200979
- // Is this regexp structure already simple
200980
- // (has it been returned by Simplify)?
200981
- // uint8_t instead of bool to control space usage.
200982
- uint8_t simple_;
200983
-
200984
- // Flags saved from parsing and used during execution.
200985
- // (Only FoldCase is used.)
200986
- // uint16_t instead of ParseFlags to control space usage.
200987
- uint16_t parse_flags_;
200988
-
200989
- // Reference count. Exists so that SimplifyRegexp can build
200990
- // regexp structures that are dags rather than trees to avoid
200991
- // exponential blowup in space requirements.
200992
- // uint16_t to control space usage.
200993
- // The standard regexp routines will never generate a
200994
- // ref greater than the maximum repeat count (kMaxRepeat),
200995
- // but even so, Incref and Decref consult an overflow map
200996
- // when ref_ reaches kMaxRef.
200997
- uint16_t ref_;
200998
- static const uint16_t kMaxRef = 0xffff;
200999
-
201000
- // Subexpressions.
201001
- // uint16_t to control space usage.
201002
- // Concat and Alternate handle larger numbers of subexpressions
201003
- // by building concatenation or alternation trees.
201004
- // Other routines should call Concat or Alternate instead of
201005
- // filling in sub() by hand.
201006
- uint16_t nsub_;
201007
- static const uint16_t kMaxNsub = 0xffff;
201008
- union {
201009
- Regexp** submany_; // if nsub_ > 1
201010
- Regexp* subone_; // if nsub_ == 1
201011
- };
201012
-
201013
- // Extra space for parse and teardown stacks.
201014
- Regexp* down_;
201015
-
201016
- // Arguments to operator. See description of operators above.
201017
- union {
201018
- repeat_t repeat_;
201019
- capture_t capture_;
201020
- literal_string_t literal_string_;
201021
- char_class_t char_class_;
201022
- Rune rune_; // Literal
201023
- int match_id_; // HaveMatch
201024
- void *the_union_[2]; // as big as any other element, for memset
201025
- };
201026
-
201027
- Regexp(const Regexp&) = delete;
201028
- Regexp& operator=(const Regexp&) = delete;
201029
- };
201030
-
201031
- // Character class set: contains non-overlapping, non-abutting RuneRanges.
201032
- typedef std::set<RuneRange, RuneRangeLess> RuneRangeSet;
201033
-
201034
- class CharClassBuilder {
201035
- public:
201036
- CharClassBuilder();
201037
-
201038
- typedef RuneRangeSet::iterator iterator;
201039
- iterator begin() { return ranges_.begin(); }
201040
- iterator end() { return ranges_.end(); }
201041
-
201042
- int size() { return nrunes_; }
201043
- bool empty() { return nrunes_ == 0; }
201044
- bool full() { return nrunes_ == Runemax+1; }
201045
-
201046
- bool Contains(Rune r);
201047
- bool FoldsASCII();
201048
- bool AddRange(Rune lo, Rune hi); // returns whether class changed
201049
- CharClassBuilder* Copy();
201050
- void AddCharClass(CharClassBuilder* cc);
201051
- void Negate();
201052
- void RemoveAbove(Rune r);
201053
- CharClass* GetCharClass();
201054
- void AddRangeFlags(Rune lo, Rune hi, Regexp::ParseFlags parse_flags);
201055
-
201056
- private:
201057
- static const uint32_t AlphaMask = (1<<26) - 1;
201058
- uint32_t upper_; // bitmap of A-Z
201059
- uint32_t lower_; // bitmap of a-z
201060
- int nrunes_;
201061
- RuneRangeSet ranges_;
201062
-
201063
- CharClassBuilder(const CharClassBuilder&) = delete;
201064
- CharClassBuilder& operator=(const CharClassBuilder&) = delete;
201065
- };
201066
-
201067
- // Bitwise ops on ParseFlags produce ParseFlags.
201068
- inline Regexp::ParseFlags operator|(Regexp::ParseFlags a,
201069
- Regexp::ParseFlags b) {
201070
- return static_cast<Regexp::ParseFlags>(
201071
- static_cast<int>(a) | static_cast<int>(b));
201072
- }
201073
-
201074
- inline Regexp::ParseFlags operator^(Regexp::ParseFlags a,
201075
- Regexp::ParseFlags b) {
201076
- return static_cast<Regexp::ParseFlags>(
201077
- static_cast<int>(a) ^ static_cast<int>(b));
201078
- }
201079
-
201080
- inline Regexp::ParseFlags operator&(Regexp::ParseFlags a,
201081
- Regexp::ParseFlags b) {
201082
- return static_cast<Regexp::ParseFlags>(
201083
- static_cast<int>(a) & static_cast<int>(b));
201084
- }
201085
-
201086
- inline Regexp::ParseFlags operator~(Regexp::ParseFlags a) {
201087
- // Attempting to produce a value out of enum's range has undefined behaviour.
201088
- return static_cast<Regexp::ParseFlags>(
201089
- ~static_cast<int>(a) & static_cast<int>(Regexp::AllParseFlags));
201090
- }
201091
-
201092
- } // namespace duckdb_re2
201093
-
201094
- #endif // RE2_REGEXP_H_
201095
-
201096
-
201097
- // LICENSE_CHANGE_END
201098
-
201099
-
201100
201197
  namespace duckdb_re2 {
201101
201198
 
201102
201199
  struct Job {