Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Flesh out the regex search APIs #35

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
image: Visual Studio 2015

environment:
nodejs_version: "6"
nodejs_version: "8"

platform:
- x86
Expand Down
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ dependencies:
pre:
- git submodule update --init
- curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
- nvm install 6.9.4
- nvm use 6.9.4
- nvm install 8.4
- nvm use 8.4

override:
- npm install
Expand Down
106 changes: 86 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,53 @@ if (process.env.SUPERSTRING_USE_BROWSER_VERSION) {
binding = require('./browser');

const {TextBuffer, Patch} = binding
const {find, findSync, findAllSync, findWordsWithSubsequenceInRange} = TextBuffer.prototype
const {findSync, findAllSync, findWordsWithSubsequenceInRange} = TextBuffer.prototype
const DEFAULT_RANGE = Object.freeze({start: {row: 0, column: 0}, end: {row: Infinity, column: Infinity}})

TextBuffer.prototype.findSync = function (pattern) {
TextBuffer.prototype.findInRangeSync = function (pattern, range) {
if (pattern.source) pattern = pattern.source
const result = findSync.call(this, pattern)
const result = findSync.call(this, pattern, range)
if (typeof result === 'string') {
throw new Error(result);
} else {
return result
}
}

TextBuffer.prototype.findAllSync = function (pattern) {
TextBuffer.prototype.findSync = function (pattern, range) {
return this.findInRangeSync(pattern, DEFAULT_RANGE)
}

TextBuffer.prototype.findAllInRangeSync = function (pattern, range) {
if (pattern.source) pattern = pattern.source
const result = findAllSync.call(this, pattern)
const result = findAllSync.call(this, pattern, range)
if (typeof result === 'string') {
throw new Error(result);
} else {
return result
}
}

TextBuffer.prototype.findAllSync = function (pattern, range) {
return this.findAllInRangeSync(pattern, DEFAULT_RANGE)
}

TextBuffer.prototype.find = function (pattern) {
return new Promise(resolve => resolve(this.findSync(pattern)))
}

TextBuffer.prototype.findInRange = function (pattern, range) {
return new Promise(resolve => resolve(this.findInRangeSync(pattern, range)))
}

TextBuffer.prototype.findAll = function (pattern) {
return new Promise(resolve => resolve(this.findAllSync(pattern)))
}

TextBuffer.prototype.findAllInRange = function (pattern, range) {
return new Promise(resolve => resolve(this.findAllInRangeSync(pattern, range)))
}

TextBuffer.prototype.findWordsWithSubsequence = function (query, extraWordCharacters, maxCount) {
const range = {start: {row: 0, column: 0}, end: this.getExtent()}
return Promise.resolve(
Expand Down Expand Up @@ -69,7 +90,10 @@ if (process.env.SUPERSTRING_USE_BROWSER_VERSION) {
}

const {TextBuffer, TextWriter, TextReader} = binding
const {load, save, find, findAllSync, findWordsWithSubsequenceInRange, baseTextMatchesFile} = TextBuffer.prototype
const {
load, save, baseTextMatchesFile,
find, findAll, findSync, findAllSync, findWordsWithSubsequenceInRange
} = TextBuffer.prototype

TextBuffer.prototype.load = function (source, options, progressCallback) {
if (typeof options !== 'object') {
Expand Down Expand Up @@ -157,26 +181,44 @@ if (process.env.SUPERSTRING_USE_BROWSER_VERSION) {
}

TextBuffer.prototype.find = function (pattern) {
return this.findInRange(pattern, null)
}

TextBuffer.prototype.findInRange = function (pattern, range) {
return new Promise((resolve, reject) => {
find.call(this, pattern, (error, result) => {
error ?
reject(error) :
resolve(result)
})
error ? reject(error) : resolve(result.length > 0 ? interpretRange(result) : null)
}, range)
})
}

TextBuffer.prototype.findAll = function (pattern) {
return this.findAllInRange(pattern, null)
}

TextBuffer.prototype.findAllInRange = function (pattern, range) {
return new Promise((resolve, reject) => {
findAll.call(this, pattern, (error, result) => {
error ? reject(error) : resolve(interpretRangeArray(result))
}, range)
})
}

TextBuffer.prototype.findSync = function (pattern) {
return this.findInRangeSync(pattern, null)
}

TextBuffer.prototype.findInRangeSync = function (pattern, range) {
const result = findSync.call(this, pattern, range)
return result.length > 0 ? interpretRange(result) : null
}

TextBuffer.prototype.findAllSync = function (pattern) {
const rawData = findAllSync.call(this, pattern)
const result = new Array(rawData.length / 4)
let rawIndex = 0
for (let matchIndex = 0, n = result.length; matchIndex < n; matchIndex++) {
result[matchIndex] = {
start: {row: rawData[rawIndex++], column: rawData[rawIndex++]},
end: {row: rawData[rawIndex++], column: rawData[rawIndex++]}
}
}
return result
return interpretRangeArray(findAllSync.call(this, pattern, null))
}

TextBuffer.prototype.findAllInRangeSync = function (pattern, range) {
return interpretRangeArray(findAllSync.call(this, pattern, range))
}

TextBuffer.prototype.findWordsWithSubsequence = function (query, extraWordCharacters, maxCount) {
Expand Down Expand Up @@ -216,6 +258,30 @@ if (process.env.SUPERSTRING_USE_BROWSER_VERSION) {
}
})
}

function interpretRangeArray (rawData) {
const rangeCount = rawData.length / 4
const ranges = new Array(rangeCount)
let rawIndex = 0
for (let rangeIndex = 0; rangeIndex < rangeCount; rangeIndex++) {
ranges[rangeIndex] = interpretRange(rawData, rawIndex)
rawIndex += 4
}
return ranges
}

function interpretRange (rawData, index = 0) {
return {
start: {
row: rawData[index],
column: rawData[index + 1]
},
end: {
row: rawData[index + 2],
column: rawData[index + 3]
}
}
}
}

function normalizeEncoding(encoding) {
Expand Down
8 changes: 4 additions & 4 deletions src/bindings/em/text-buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ static TextBuffer *construct(const std::string &text) {
return new TextBuffer(u16string(text.begin(), text.end()));
}

static emscripten::val find_sync(TextBuffer &buffer, std::string js_pattern) {
static emscripten::val find_sync(TextBuffer &buffer, std::string js_pattern, Range range) {
u16string pattern(js_pattern.begin(), js_pattern.end());
u16string error_message;
Regex regex(pattern, &error_message);
if (!error_message.empty()) {
return emscripten::val(string(error_message.begin(), error_message.end()));
}

auto result = buffer.find(regex);
auto result = buffer.find(regex, range);
if (result) {
return emscripten::val(*result);
}

return emscripten::val::null();
}

static emscripten::val find_all_sync(TextBuffer &buffer, std::string js_pattern) {
static emscripten::val find_all_sync(TextBuffer &buffer, std::string js_pattern, Range range) {
u16string pattern(js_pattern.begin(), js_pattern.end());
u16string error_message;
Regex regex(pattern, &error_message);
if (!error_message.empty()) {
return emscripten::val(string(error_message.begin(), error_message.end()));
}

return em_transmit(buffer.find_all(regex));
return em_transmit(buffer.find_all(regex, range));
}

static emscripten::val line_ending_for_row(TextBuffer &buffer, uint32_t row) {
Expand Down
Loading