1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_BUILTINS_BUILTINS_REGEXP_INL_H
17 #define ECMASCRIPT_BUILTINS_BUILTINS_REGEXP_INL_H
18 
19 #include "ecmascript/ecma_context.h"
20 
21 namespace panda::ecmascript::builtins {
22 
23 /* static */
24 template <int N>
GetCapture(JSThread *thread)25 JSTaggedValue RegExpGlobalResult::GetCapture(JSThread *thread)
26 {
27     JSHandle<builtins::RegExpGlobalResult> globalTable(thread->GetCurrentEcmaContext()->GetRegExpGlobalResult());
28     JSTaggedValue res = globalTable->Get(CAPTURE_START_INDEX + N - 1);
29     int captureNum = globalTable->GetTotalCaptureCounts().GetInt();
30     if (res.IsHole() && (N < captureNum)) {
31         int startIndex = globalTable->GetStartOfCaptureIndex(N).GetInt();
32         int endIndex = globalTable->GetEndOfCaptureIndex(N).GetInt();
33         int len = endIndex - startIndex;
34         if (len < 0) {
35             res = JSTaggedValue::Undefined();
36         } else {
37             res = JSTaggedValue(EcmaStringAccessor::FastSubString(thread->GetEcmaVM(),
38                 JSHandle<EcmaString>(thread, EcmaString::Cast(globalTable->GetInputString())),
39                 static_cast<uint32_t>(startIndex), static_cast<uint32_t>(len)));
40         }
41         globalTable->Set(thread, CAPTURE_START_INDEX + N - 1, res);
42     } else if (res.IsHole()) {
43         res = thread->GetEcmaVM()->GetFactory()->GetEmptyString().GetTaggedValue();
44         globalTable->Set(thread, CAPTURE_START_INDEX + N - 1, res);
45     }
46     return res;
47 }
48 
49 }  // namespace panda::ecmascript::builtins
50 #endif  // ECMASCRIPT_BUILTINS_BUILTINS_REGEXP_INL_H
51