summaryrefslogtreecommitdiff
path: root/node_modules/locutus/php/strings/strlen.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/locutus/php/strings/strlen.js')
-rw-r--r--node_modules/locutus/php/strings/strlen.js76
1 files changed, 76 insertions, 0 deletions
diff --git a/node_modules/locutus/php/strings/strlen.js b/node_modules/locutus/php/strings/strlen.js
new file mode 100644
index 0000000..7c664d2
--- /dev/null
+++ b/node_modules/locutus/php/strings/strlen.js
@@ -0,0 +1,76 @@
+'use strict';
+
+module.exports = function strlen(string) {
+ // discuss at: http://locutus.io/php/strlen/
+ // original by: Kevin van Zonneveld (http://kvz.io)
+ // improved by: Sakimori
+ // improved by: Kevin van Zonneveld (http://kvz.io)
+ // input by: Kirk Strobeck
+ // bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
+ // revised by: Brett Zamir (http://brett-zamir.me)
+ // note 1: May look like overkill, but in order to be truly faithful to handling all Unicode
+ // note 1: characters and to this function in PHP which does not count the number of bytes
+ // note 1: but counts the number of characters, something like this is really necessary.
+ // example 1: strlen('Kevin van Zonneveld')
+ // returns 1: 19
+ // example 2: ini_set('unicode.semantics', 'on')
+ // example 2: strlen('A\ud87e\udc04Z')
+ // returns 2: 3
+
+ var str = string + '';
+
+ var iniVal = (typeof require !== 'undefined' ? require('../info/ini_get')('unicode.semantics') : undefined) || 'off';
+ if (iniVal === 'off') {
+ return str.length;
+ }
+
+ var i = 0;
+ var lgth = 0;
+
+ var getWholeChar = function getWholeChar(str, i) {
+ var code = str.charCodeAt(i);
+ var next = '';
+ var prev = '';
+ if (code >= 0xD800 && code <= 0xDBFF) {
+ // High surrogate (could change last hex to 0xDB7F to
+ // treat high private surrogates as single characters)
+ if (str.length <= i + 1) {
+ throw new Error('High surrogate without following low surrogate');
+ }
+ next = str.charCodeAt(i + 1);
+ if (next < 0xDC00 || next > 0xDFFF) {
+ throw new Error('High surrogate without following low surrogate');
+ }
+ return str.charAt(i) + str.charAt(i + 1);
+ } else if (code >= 0xDC00 && code <= 0xDFFF) {
+ // Low surrogate
+ if (i === 0) {
+ throw new Error('Low surrogate without preceding high surrogate');
+ }
+ prev = str.charCodeAt(i - 1);
+ if (prev < 0xD800 || prev > 0xDBFF) {
+ // (could change last hex to 0xDB7F to treat high private surrogates
+ // as single characters)
+ throw new Error('Low surrogate without preceding high surrogate');
+ }
+ // We can pass over low surrogates now as the second
+ // component in a pair which we have already processed
+ return false;
+ }
+ return str.charAt(i);
+ };
+
+ for (i = 0, lgth = 0; i < str.length; i++) {
+ if (getWholeChar(str, i) === false) {
+ continue;
+ }
+ // Adapt this line at the top of any loop, passing in the whole string and
+ // the current iteration and returning a variable to represent the individual character;
+ // purpose is to treat the first part of a surrogate pair as the whole character and then
+ // ignore the second part
+ lgth++;
+ }
+
+ return lgth;
+};
+//# sourceMappingURL=strlen.js.map \ No newline at end of file