mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Update RapydScript
This commit is contained in:
parent
bbd635c57b
commit
388c6859bb
Binary file not shown.
@ -35,6 +35,7 @@ _re_cache_map = {}
|
|||||||
_re_cache_items = v'[]'
|
_re_cache_items = v'[]'
|
||||||
|
|
||||||
error = SyntaxError # This is the error JS throws for invalid regexps
|
error = SyntaxError # This is the error JS throws for invalid regexps
|
||||||
|
has_prop = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty)
|
||||||
|
|
||||||
def _expand(groups, repl, group_name_map):
|
def _expand(groups, repl, group_name_map):
|
||||||
i = 0
|
i = 0
|
||||||
@ -68,7 +69,7 @@ def _expand(groups, repl, group_name_map):
|
|||||||
return '\\'
|
return '\\'
|
||||||
if '"\''.indexOf(q) is not -1:
|
if '"\''.indexOf(q) is not -1:
|
||||||
return q
|
return q
|
||||||
if _ASCII_CONTROL_CHARS.hasOwnProperty(q):
|
if _ASCII_CONTROL_CHARS[q]:
|
||||||
return String.fromCharCode(_ASCII_CONTROL_CHARS[q])
|
return String.fromCharCode(_ASCII_CONTROL_CHARS[q])
|
||||||
if '0' <= q <= '9':
|
if '0' <= q <= '9':
|
||||||
ans = read_digits(Number.MAX_VALUE, _NUM_PAT, 10, Number.MAX_VALUE, q)
|
ans = read_digits(Number.MAX_VALUE, _NUM_PAT, 10, Number.MAX_VALUE, q)
|
||||||
@ -81,7 +82,7 @@ def _expand(groups, repl, group_name_map):
|
|||||||
i += m[0].length
|
i += m[0].length
|
||||||
gn = m[1]
|
gn = m[1]
|
||||||
if isNaN(parseInt(gn, 10)):
|
if isNaN(parseInt(gn, 10)):
|
||||||
if not Object.prototype.hasOwnProperty.call(group_name_map, gn):
|
if not has_prop(group_name_map, gn):
|
||||||
return ''
|
return ''
|
||||||
gn = group_name_map[gn][-1]
|
gn = group_name_map[gn][-1]
|
||||||
return groups[gn] or ''
|
return groups[gn] or ''
|
||||||
@ -112,7 +113,7 @@ def _expand(groups, repl, group_name_map):
|
|||||||
return '\\N{' + name
|
return '\\N{' + name
|
||||||
next()
|
next()
|
||||||
key = (name or '').toLowerCase()
|
key = (name or '').toLowerCase()
|
||||||
if not name or not Object.prototype.hasOwnProperty.call(_ALIAS_MAP, key):
|
if not name or not has_prop(_ALIAS_MAP, key):
|
||||||
return '\\N{' + name + '}'
|
return '\\N{' + name + '}'
|
||||||
code = _ALIAS_MAP[key]
|
code = _ALIAS_MAP[key]
|
||||||
if code <= 0xFFFF:
|
if code <= 0xFFFF:
|
||||||
@ -177,7 +178,7 @@ def transform_regex(source, flags):
|
|||||||
flgs = source[pos+1:close]
|
flgs = source[pos+1:close]
|
||||||
for v'var i = 0; i < flgs.length; i++':
|
for v'var i = 0; i < flgs.length; i++':
|
||||||
q = flgs[i] # noqa:undef
|
q = flgs[i] # noqa:undef
|
||||||
if not flag_map.hasOwnProperty(q):
|
if not has_prop(flag_map, q):
|
||||||
raise SyntaxError('Invalid flag: ' + q)
|
raise SyntaxError('Invalid flag: ' + q)
|
||||||
flags |= flag_map[q]
|
flags |= flag_map[q]
|
||||||
pos = close + 1
|
pos = close + 1
|
||||||
@ -194,7 +195,7 @@ def transform_regex(source, flags):
|
|||||||
if close is -1:
|
if close is -1:
|
||||||
raise SyntaxError('Named group not closed, expecting >')
|
raise SyntaxError('Named group not closed, expecting >')
|
||||||
name = source[pos+1:close]
|
name = source[pos+1:close]
|
||||||
if not Object.prototype.hasOwnProperty.call(group_map, name):
|
if not has_prop(group_map, name):
|
||||||
group_map[name] = v'[]'
|
group_map[name] = v'[]'
|
||||||
group_map[name].push(v'++group_count')
|
group_map[name].push(v'++group_count')
|
||||||
pos = close + 1
|
pos = close + 1
|
||||||
@ -206,7 +207,7 @@ def transform_regex(source, flags):
|
|||||||
if not isNaN(parseInt(name, 10)):
|
if not isNaN(parseInt(name, 10)):
|
||||||
ans += '\\' + name
|
ans += '\\' + name
|
||||||
else:
|
else:
|
||||||
if not Object.prototype.hasOwnProperty.call(group_map, name):
|
if not has_prop(group_map, name):
|
||||||
raise SyntaxError('Invalid back-reference. The named group: ' + name + ' has not yet been defined.')
|
raise SyntaxError('Invalid back-reference. The named group: ' + name + ' has not yet been defined.')
|
||||||
ans += '\\' + group_map[name][-1]
|
ans += '\\' + group_map[name][-1]
|
||||||
pos = close + 1
|
pos = close + 1
|
||||||
@ -265,7 +266,7 @@ class MatchObject:
|
|||||||
def _group_number(self, g):
|
def _group_number(self, g):
|
||||||
if type(g) is 'number':
|
if type(g) is 'number':
|
||||||
return g
|
return g
|
||||||
if Object.prototype.hasOwnProperty.call(self.re.group_name_map, g):
|
if has_prop(self.re.group_name_map, g):
|
||||||
return self.re.group_name_map[g][-1]
|
return self.re.group_name_map[g][-1]
|
||||||
return g
|
return g
|
||||||
|
|
||||||
@ -274,7 +275,7 @@ class MatchObject:
|
|||||||
if type(q) is 'number' and -1 < q < self._groups.length:
|
if type(q) is 'number' and -1 < q < self._groups.length:
|
||||||
val = self._groups[q]
|
val = self._groups[q]
|
||||||
else:
|
else:
|
||||||
if Object.prototype.hasOwnProperty.call(self.re.group_name_map, q):
|
if has_prop(self.re.group_name_map, q):
|
||||||
val = self._groups[self.re.group_name_map[q][-1]]
|
val = self._groups[self.re.group_name_map[q][-1]]
|
||||||
if val is undefined:
|
if val is undefined:
|
||||||
val = defval
|
val = defval
|
||||||
@ -317,7 +318,7 @@ class MatchObject:
|
|||||||
ans = {}
|
ans = {}
|
||||||
for v"var i = 0; i < names.length; i++":
|
for v"var i = 0; i < names.length; i++":
|
||||||
name = names[i] # noqa:undef
|
name = names[i] # noqa:undef
|
||||||
if Object.prototype.hasOwnProperty.call(gnm, name):
|
if has_prop(gnm, name):
|
||||||
val = self._groups[gnm[name][-1]]
|
val = self._groups[gnm[name][-1]]
|
||||||
if val is undefined:
|
if val is undefined:
|
||||||
val = defval
|
val = defval
|
||||||
@ -326,7 +327,7 @@ class MatchObject:
|
|||||||
|
|
||||||
def captures(self, group_name):
|
def captures(self, group_name):
|
||||||
ans = []
|
ans = []
|
||||||
if not Object.prototype.hasOwnProperty.call(self.re.group_name_map, group_name):
|
if not has_prop(self.re.group_name_map, group_name):
|
||||||
return ans
|
return ans
|
||||||
groups = self.re.group_name_map[group_name]
|
groups = self.re.group_name_map[group_name]
|
||||||
for v'var i = 0; i < groups.length; i++':
|
for v'var i = 0; i < groups.length; i++':
|
||||||
@ -384,18 +385,17 @@ class RegexObject:
|
|||||||
return ρσ_list_decorate(string.match(self._pattern) or v'[]')
|
return ρσ_list_decorate(string.match(self._pattern) or v'[]')
|
||||||
|
|
||||||
def finditer(self, string):
|
def finditer(self, string):
|
||||||
pat = RegExp(this._pattern.source, this._modifiers) # We have to do this since lastIndex is mutable
|
# We have to copy pat since lastIndex is mutable
|
||||||
return {
|
pat = RegExp(this._pattern.source, this._modifiers) # noqa: unused-local
|
||||||
'_string':string,
|
ans = v"{'_string':string, '_r':pat, '_self':self}"
|
||||||
'_r': pat,
|
ans[ρσ_iterator_symbol] = def():
|
||||||
'_self': self,
|
return this
|
||||||
ρσ_iterator_symbol: def (): return this;,
|
ans['next'] = def():
|
||||||
'next': def ():
|
m = this._r.exec(this._string)
|
||||||
m = this._r.exec(this._string)
|
if m is None:
|
||||||
if m is None:
|
return v"{'done':true}"
|
||||||
return {'done':True}
|
return v"{'done':false, 'value':new MatchObject(this._self, m, 0, null)}"
|
||||||
return {'done':False, 'value':MatchObject(this._self, m, 0, None)}
|
return ans
|
||||||
}
|
|
||||||
|
|
||||||
def subn(self, repl, string, count=0):
|
def subn(self, repl, string, count=0):
|
||||||
expand = _expand
|
expand = _expand
|
||||||
@ -426,7 +426,7 @@ def _get_from_cache(pattern, flags):
|
|||||||
if isinstance(pattern, RegExp):
|
if isinstance(pattern, RegExp):
|
||||||
pattern = pattern.source
|
pattern = pattern.source
|
||||||
key = JSON.stringify(v'[pattern, flags]')
|
key = JSON.stringify(v'[pattern, flags]')
|
||||||
if Object.prototype.hasOwnProperty.call(_re_cache_map, key):
|
if has_prop(_re_cache_map, key):
|
||||||
return _re_cache_map[key]
|
return _re_cache_map[key]
|
||||||
if _re_cache_items.length >= 100:
|
if _re_cache_items.length >= 100:
|
||||||
v'delete _re_cache_map[_re_cache_items.shift()]'
|
v'delete _re_cache_map[_re_cache_items.shift()]'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user