-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #143 - Initial implementation of \iz and \izj
- Loading branch information
Showing
2 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
function isPrintable (ch) { | ||
return (ch >= 32 && ch <= 126); | ||
} | ||
|
||
function parseOptions (options) { | ||
const opts = { | ||
minLength: 15, | ||
maxLength: 128, | ||
filter: false, | ||
urls: false, | ||
base: 0 | ||
}; | ||
if (typeof options === 'object') { | ||
for (let key of Object.keys(options)) { | ||
opts[key] = options[key]; | ||
} | ||
} | ||
return opts; | ||
} | ||
|
||
function parseStrings (data, options) { | ||
const opt = parseOptions(options); | ||
const strs = []; | ||
let str = ''; | ||
let off = 0; | ||
let cur = 0; | ||
data.forEach(ch => { | ||
if (isPrintable(ch)) { | ||
if (str === '') { | ||
cur = off; | ||
} | ||
str += String.fromCharCode(ch); | ||
} else { | ||
if (str.length > opt.minLength && str.length < opt.maxLength) { | ||
let valid = true; | ||
if (opt.filter && !isValidString(str)) { | ||
valid = false; | ||
} | ||
if (opt.urls && !isValidURL(str)) { | ||
valid = false; | ||
} | ||
if (valid) { | ||
strs.push({ base: opt.base.add(cur), text: str }); | ||
} | ||
} | ||
str = ''; | ||
} | ||
off++; | ||
}); | ||
return strs; | ||
} | ||
|
||
function isValidString (s) { | ||
if (s.indexOf('://') !== -1) { | ||
return false; | ||
} | ||
if (+s) { | ||
return false; | ||
} | ||
const invalidChars = '<\\)?@)>{~}^()=/!-"*:]%\';` $'; | ||
for (let ic of invalidChars) { | ||
if (s.indexOf(ic) !== -1) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function isValidURL (s) { | ||
if (s.indexOf('://') === -1) { | ||
return false; | ||
} | ||
const invalidChars = '<\\)?)>{~}^()=!-"*]\'` $'; | ||
for (let ic of invalidChars) { | ||
if (s.indexOf(ic) !== -1) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
module.exports = parseStrings; |