Skip to content

Commit fc8fba0

Browse files
steff456kgryte
andauthored
feat: add string/next-code-point-index
PR-URL: #1117 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Ref: #1062
1 parent 4899786 commit fc8fba0

File tree

15 files changed

+1286
-0
lines changed

15 files changed

+1286
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# nextCodePointIndex
22+
23+
> Return the position of the next Unicode code point in a string after a specified position.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' );
41+
```
42+
43+
#### nextCodePointIndex( string\[, fromIndex] )
44+
45+
Returns the position of the next Unicode code point in a string after a specified position.
46+
47+
```javascript
48+
var out = nextCodePointIndex( 'last man standing' );
49+
// returns 1
50+
```
51+
52+
By default, the function searches for a Unicode code point starting from the first index. To specify an alternative starting search index, provide a `fromIndex` argument.
53+
54+
```javascript
55+
var out = nextCodePointIndex( 'last man standing', 4 );
56+
// returns 5
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
## Notes
68+
69+
- If `string` is an empty string, the function returns `-1` irrespective of `fromIndex`.
70+
- If a code point does not exist after `fromIndex`, the function returns `-1`.
71+
- Note that `fromIndex` does **not** refer to a visual character position, but to an index in the ordered sequence of [UTF-16][utf-16] code units.
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<!-- Package usage examples. -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var nextCodePointIndex = require( '@stdlib/string/next-code-point-index' );
87+
88+
var out = nextCodePointIndex( 'last man standing', 4 );
89+
// returns 5
90+
91+
out = nextCodePointIndex( 'presidential election', 8 );
92+
// returns 9
93+
94+
out = nextCodePointIndex( '𐒻𐓟𐒻𐓟', 0 );
95+
// returns 2
96+
97+
out = nextCodePointIndex( '🌷', 0 );
98+
// returns -1
99+
```
100+
101+
</section>
102+
103+
<!-- /.examples -->
104+
105+
<!-- Section for describing a command-line interface. -->
106+
107+
* * *
108+
109+
<section class="cli">
110+
111+
## CLI
112+
113+
<!-- CLI usage documentation. -->
114+
115+
<section class="usage">
116+
117+
### Usage
118+
119+
```text
120+
Usage: next-code-point-index [options] [<string>]
121+
122+
Options:
123+
124+
-h, --help Print this message.
125+
-V, --version Print the package version.
126+
--from index Starting search position in string. Default: 0.
127+
```
128+
129+
</section>
130+
131+
<!-- /.usage -->
132+
133+
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="notes">
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<!-- CLI usage examples. -->
142+
143+
<section class="examples">
144+
145+
### Examples
146+
147+
```bash
148+
$ next-code-point-index --from=0 𐒻𐓟𐒻𐓟
149+
2
150+
```
151+
152+
To use as a [standard stream][standard-streams],
153+
154+
```bash
155+
$ echo -n '𐒻𐓟𐒻𐓟' | next-code-point-index --from=0
156+
2
157+
```
158+
159+
</section>
160+
161+
<!-- /.examples -->
162+
163+
</section>
164+
165+
<!-- /.cli -->
166+
167+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
168+
169+
<section class="references">
170+
171+
</section>
172+
173+
<!-- /.references -->
174+
175+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
176+
177+
<section class="related">
178+
179+
</section>
180+
181+
<!-- /.related -->
182+
183+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
184+
185+
<section class="links">
186+
187+
[standard-streams]: https://en.wikipedia.org/wiki/Standard_streams
188+
189+
[utf-16]: https://en.wikipedia.org/wiki/UTF-16
190+
191+
</section>
192+
193+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var nextCodePointIndex = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var strings;
33+
var len;
34+
var out;
35+
var i;
36+
37+
strings = [
38+
'last man standing',
39+
'presidential election',
40+
'अनुच्छेद',
41+
'🌷',
42+
'书/六書',
43+
'เ❄︎நி',
44+
'กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้กิิก้้ก็็ก็็กิิก้้ก็็กิิก้้',
45+
'書六/书六',
46+
'ܶƔλʃݖͱšɕ҆ʧѸؐҜҦɳΏ',
47+
'âݝΝ‚ҳӌݾҀƳ۵ۧ޳ǁǸΓ'
48+
];
49+
len = strings.length;
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
out = nextCodePointIndex( strings[ i%len ], 1 );
54+
if ( out !== out ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
}
58+
b.toc();
59+
if ( !isInteger( out ) ) {
60+
b.fail( 'should return an integer' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Apache-2.0
5+
*
6+
* Copyright (c) 2023 The Stdlib Authors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
27+
var CLI = require( '@stdlib/cli/ctor' );
28+
var stdin = require( '@stdlib/process/read-stdin' );
29+
var stdinStream = require( '@stdlib/streams/node/stdin' );
30+
var nextCodePointIndex = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Main execution sequence.
37+
*
38+
* @private
39+
* @returns {void}
40+
*/
41+
function main() {
42+
var flags;
43+
var args;
44+
var cli;
45+
var pos;
46+
47+
// Create a command-line interface:
48+
cli = new CLI({
49+
'pkg': require( './../package.json' ),
50+
'options': require( './../etc/cli_opts.json' ),
51+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
52+
'encoding': 'utf8'
53+
})
54+
});
55+
56+
// Get any provided command-line options:
57+
flags = cli.flags();
58+
if ( flags.help || flags.version ) {
59+
return;
60+
}
61+
if ( flags.from ) {
62+
pos = parseInt( flags.from, 10 );
63+
} else {
64+
pos = 0;
65+
}
66+
// Get any provided command-line arguments:
67+
args = cli.args();
68+
69+
// Check if we are receiving data from `stdin`...
70+
if ( stdinStream.isTTY ) {
71+
return console.log( nextCodePointIndex( args[ 0 ], pos ) ); // eslint-disable-line no-console
72+
}
73+
return stdin( onRead );
74+
75+
/**
76+
* Callback invoked upon reading from `stdin`.
77+
*
78+
* @private
79+
* @param {(Error|null)} error - error object
80+
* @param {Buffer} data - data
81+
* @returns {void}
82+
*/
83+
function onRead( error, data ) {
84+
if ( error ) {
85+
return cli.error( error );
86+
}
87+
console.log( nextCodePointIndex( data.toString(), pos ) ); // eslint-disable-line no-console
88+
}
89+
}
90+
91+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( str[, fromIndex] )
3+
Returns the position of the next Unicode code point in a string after a
4+
specified position.
5+
6+
Parameters
7+
----------
8+
str: string
9+
Input string.
10+
11+
fromIndex: integer (optional)
12+
Position. Default: 0.
13+
14+
Returns
15+
-------
16+
out: integer
17+
Next code point position.
18+
19+
Examples
20+
--------
21+
> var out = {{alias}}( 'last man standing', 4 )
22+
5
23+
> out = {{alias}}( 'presidential election', 8 )
24+
9
25+
> out = {{alias}}( '𐒻𐓟𐒻𐓟', 0 )
26+
2
27+
> out = {{alias}}( '🌷' )
28+
-1
29+
30+
See Also
31+
--------

0 commit comments

Comments
 (0)