alphanumeric.js

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-10-12 00:00:00 +00:00
parent c666d426f7
commit 5b7094fb68
2 changed files with 52 additions and 0 deletions

View File

@@ -13190,6 +13190,8 @@ ERROR: package host-nodejs installs executables without proper RPATH
Examples:
* String
** link:rootfs_overlay/lkmc/nodejs/alphanumeric.js[]: https://stackoverflow.com/questions/4444477/how-to-tell-if-a-string-contains-a-certain-character-in-javascript/58359106#58359106
* `process`
** link:rootfs_overlay/lkmc/nodejs/command_line_arguments.js[]
* `fs`

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env node
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
const assert = require('assert');
const char_is_alphanumeric = function(c) {
let code = c.codePointAt(0);
return (
// 0-9
(code > 47 && code < 58) ||
// A-Z
(code > 64 && code < 91) ||
// a-z
(code > 96 && code < 123)
)
}
const is_alphanumeric = function (str) {
for (let c of str) {
if (!char_is_alphanumeric(c)) {
return false;
}
}
return true;
};
// Arbitrarily defined as alphanumeric or '-' or '_'.
const is_almost_alphanumeric = function (str) {
for (let c of str) {
if (
!char_is_alphanumeric(c) &&
!is_almost_alphanumeric.almost_chars.has(c)
) {
return false;
}
}
return true;
};
is_almost_alphanumeric.almost_chars = new Set(['-', '_']);
assert( is_alphanumeric('aB0'));
assert(!is_alphanumeric('aB0_-'));
assert(!is_alphanumeric('aB0_-*'));
assert(!is_alphanumeric('你好'));
assert( is_almost_alphanumeric('aB0'));
assert( is_almost_alphanumeric('aB0_-'));
assert(!is_almost_alphanumeric('aB0_-*'));
assert(!is_almost_alphanumeric('你好'));