From c6c4d6413ebc556f497cadc38f1449b1f0c7df81 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 8 Aug 2012 09:11:32 -0600 Subject: [PATCH] Optimization attempt: Captures contain start/end position instead of text --- Atom/src/OnigRegexpExtension.mm | 15 ++++++++++----- spec/stdlib/onig-reg-exp-spec.coffee | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Atom/src/OnigRegexpExtension.mm b/Atom/src/OnigRegexpExtension.mm index a4ad21cca..b97638e7e 100644 --- a/Atom/src/OnigRegexpExtension.mm +++ b/Atom/src/OnigRegexpExtension.mm @@ -50,9 +50,8 @@ public: CefRefPtr BuildCaptureTree(OnigResult *result, int &index) { int currentIndex = index++; - NSString *text = [result stringAt:currentIndex]; int startPosition = [result locationAt:currentIndex]; - int endPosition = startPosition + [text length]; + int endPosition = startPosition + [result lengthAt:currentIndex]; CefRefPtr childCaptures; @@ -65,11 +64,17 @@ public: childCaptures->SetValue(childCaptures->GetArrayLength(), BuildCaptureTree(result, index)); } } - + CefRefPtr tree = CefV8Value::CreateObject(NULL, NULL); + tree->SetValue("index", CefV8Value::CreateInt(currentIndex), V8_PROPERTY_ATTRIBUTE_NONE); - tree->SetValue("text", CefV8Value::CreateString([text UTF8String]), V8_PROPERTY_ATTRIBUTE_NONE); - tree->SetValue("position", CefV8Value::CreateInt(startPosition), V8_PROPERTY_ATTRIBUTE_NONE); + tree->SetValue("start", CefV8Value::CreateInt(startPosition), V8_PROPERTY_ATTRIBUTE_NONE); + tree->SetValue("end", CefV8Value::CreateInt(endPosition), V8_PROPERTY_ATTRIBUTE_NONE); + + if (currentIndex == 0) { + tree->SetValue("text", CefV8Value::CreateString([[result stringAt:currentIndex] UTF8String]), V8_PROPERTY_ATTRIBUTE_NONE); + } + if (childCaptures.get()) tree->SetValue("captures", childCaptures, V8_PROPERTY_ATTRIBUTE_NONE); return tree; } diff --git a/spec/stdlib/onig-reg-exp-spec.coffee b/spec/stdlib/onig-reg-exp-spec.coffee index 118012e99..590b7c8da 100644 --- a/spec/stdlib/onig-reg-exp-spec.coffee +++ b/spec/stdlib/onig-reg-exp-spec.coffee @@ -13,27 +13,28 @@ describe "OnigRegExp", -> expect(result).toBeNull() describe ".getCaptureTree(string, index)", -> - it "returns match with nested capture groups organized into a tree", -> + fit "returns match with nested capture groups organized into a tree", -> regex = new OnigRegExp("a((bc)d)e(f(g)(h))(?=ij)") tree = regex.getCaptureTree("abcdefghij") expect(tree).toEqual text: "abcdefgh" index: 0 - position: 0 + start: 0 + end: 8 captures: [ { - text: "bcd" index: 1 - position: 1 - captures: [{ text: "bc", index: 2, position: 1 }] + start: 1 + end: 4 + captures: [{ index: 2, start: 1, end: 3 }] }, { - text: "fgh" index: 3 - position: 5 + start: 5 + end: 8 captures: [ - { text: "g", index: 4, position: 6 } - { text: "h", index: 5, position: 7 } + { index: 4, start: 6, end: 7 } + { index: 5, start: 7, end: 8 } ] } ]