11cb0ef41Sopenharmony_ci
21cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
31cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
41cb0ef41Sopenharmony_ci// found in the LICENSE file.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci#ifndef INCLUDE_V8_REGEXP_H_
71cb0ef41Sopenharmony_ci#define INCLUDE_V8_REGEXP_H_
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "v8-local-handle.h"  // NOLINT(build/include_directory)
101cb0ef41Sopenharmony_ci#include "v8-object.h"        // NOLINT(build/include_directory)
111cb0ef41Sopenharmony_ci#include "v8config.h"         // NOLINT(build/include_directory)
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciclass Context;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci/**
181cb0ef41Sopenharmony_ci * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
191cb0ef41Sopenharmony_ci */
201cb0ef41Sopenharmony_ciclass V8_EXPORT RegExp : public Object {
211cb0ef41Sopenharmony_ci public:
221cb0ef41Sopenharmony_ci  /**
231cb0ef41Sopenharmony_ci   * Regular expression flag bits. They can be or'ed to enable a set
241cb0ef41Sopenharmony_ci   * of flags.
251cb0ef41Sopenharmony_ci   * The kLinear value ('l') is experimental and can only be used with
261cb0ef41Sopenharmony_ci   * --enable-experimental-regexp-engine.  RegExps with kLinear flag are
271cb0ef41Sopenharmony_ci   *  guaranteed to be executed in asymptotic linear time wrt. the length of
281cb0ef41Sopenharmony_ci   *  the subject string.
291cb0ef41Sopenharmony_ci   */
301cb0ef41Sopenharmony_ci  enum Flags {
311cb0ef41Sopenharmony_ci    kNone = 0,
321cb0ef41Sopenharmony_ci    kGlobal = 1 << 0,
331cb0ef41Sopenharmony_ci    kIgnoreCase = 1 << 1,
341cb0ef41Sopenharmony_ci    kMultiline = 1 << 2,
351cb0ef41Sopenharmony_ci    kSticky = 1 << 3,
361cb0ef41Sopenharmony_ci    kUnicode = 1 << 4,
371cb0ef41Sopenharmony_ci    kDotAll = 1 << 5,
381cb0ef41Sopenharmony_ci    kLinear = 1 << 6,
391cb0ef41Sopenharmony_ci    kHasIndices = 1 << 7,
401cb0ef41Sopenharmony_ci    kUnicodeSets = 1 << 8,
411cb0ef41Sopenharmony_ci  };
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  static constexpr int kFlagCount = 9;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  /**
461cb0ef41Sopenharmony_ci   * Creates a regular expression from the given pattern string and
471cb0ef41Sopenharmony_ci   * the flags bit field. May throw a JavaScript exception as
481cb0ef41Sopenharmony_ci   * described in ECMA-262, 15.10.4.1.
491cb0ef41Sopenharmony_ci   *
501cb0ef41Sopenharmony_ci   * For example,
511cb0ef41Sopenharmony_ci   *   RegExp::New(v8::String::New("foo"),
521cb0ef41Sopenharmony_ci   *               static_cast<RegExp::Flags>(kGlobal | kMultiline))
531cb0ef41Sopenharmony_ci   * is equivalent to evaluating "/foo/gm".
541cb0ef41Sopenharmony_ci   */
551cb0ef41Sopenharmony_ci  static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
561cb0ef41Sopenharmony_ci                                                      Local<String> pattern,
571cb0ef41Sopenharmony_ci                                                      Flags flags);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  /**
601cb0ef41Sopenharmony_ci   * Like New, but additionally specifies a backtrack limit. If the number of
611cb0ef41Sopenharmony_ci   * backtracks done in one Exec call hits the limit, a match failure is
621cb0ef41Sopenharmony_ci   * immediately returned.
631cb0ef41Sopenharmony_ci   */
641cb0ef41Sopenharmony_ci  static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> NewWithBacktrackLimit(
651cb0ef41Sopenharmony_ci      Local<Context> context, Local<String> pattern, Flags flags,
661cb0ef41Sopenharmony_ci      uint32_t backtrack_limit);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  /**
691cb0ef41Sopenharmony_ci   * Executes the current RegExp instance on the given subject string.
701cb0ef41Sopenharmony_ci   * Equivalent to RegExp.prototype.exec as described in
711cb0ef41Sopenharmony_ci   *
721cb0ef41Sopenharmony_ci   *   https://tc39.es/ecma262/#sec-regexp.prototype.exec
731cb0ef41Sopenharmony_ci   *
741cb0ef41Sopenharmony_ci   * On success, an Array containing the matched strings is returned. On
751cb0ef41Sopenharmony_ci   * failure, returns Null.
761cb0ef41Sopenharmony_ci   *
771cb0ef41Sopenharmony_ci   * Note: modifies global context state, accessible e.g. through RegExp.input.
781cb0ef41Sopenharmony_ci   */
791cb0ef41Sopenharmony_ci  V8_WARN_UNUSED_RESULT MaybeLocal<Object> Exec(Local<Context> context,
801cb0ef41Sopenharmony_ci                                                Local<String> subject);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  /**
831cb0ef41Sopenharmony_ci   * Returns the value of the source property: a string representing
841cb0ef41Sopenharmony_ci   * the regular expression.
851cb0ef41Sopenharmony_ci   */
861cb0ef41Sopenharmony_ci  Local<String> GetSource() const;
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  /**
891cb0ef41Sopenharmony_ci   * Returns the flags bit field.
901cb0ef41Sopenharmony_ci   */
911cb0ef41Sopenharmony_ci  Flags GetFlags() const;
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  V8_INLINE static RegExp* Cast(Value* value) {
941cb0ef41Sopenharmony_ci#ifdef V8_ENABLE_CHECKS
951cb0ef41Sopenharmony_ci    CheckCast(value);
961cb0ef41Sopenharmony_ci#endif
971cb0ef41Sopenharmony_ci    return static_cast<RegExp*>(value);
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci private:
1011cb0ef41Sopenharmony_ci  static void CheckCast(Value* obj);
1021cb0ef41Sopenharmony_ci};
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci}  // namespace v8
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci#endif  // INCLUDE_V8_REGEXP_H_
107