diff --git a/lib/node_modules/@stdlib/complex/base/parse/lib/main.js b/lib/node_modules/@stdlib/complex/base/parse/lib/main.js index 4cfa330cbc21..34f06123591e 100644 --- a/lib/node_modules/@stdlib/complex/base/parse/lib/main.js +++ b/lib/node_modules/@stdlib/complex/base/parse/lib/main.js @@ -20,8 +20,8 @@ // MODULES // -var replace = require( '@stdlib/string/replace' ); -var Number = require( '@stdlib/number/ctor' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var regexp = require( './regexp.js' ); // MAIN // @@ -37,7 +37,7 @@ var Number = require( '@stdlib/number/ctor' ); * - **im**: imaginary component * * @param {string} str - input string -* @returns {ComplexLike} an object containing real and imaginary parts +* @returns {(ComplexLike|null)} an object containing real and imaginary parts * * @example * var str = '4 + 6i'; @@ -46,72 +46,34 @@ var Number = require( '@stdlib/number/ctor' ); * // returns { 're': 4, 'im': 6 } */ function parse( str ) { - var imaginaryParts = []; - var currentToken = ''; - var isImaginary; - var realParts = []; - var parts = []; - var valid = true; - var value; - var part; - var re = 0; - var im = 0; + var match; + var re; + var im; - var i; - if ( typeof str !== 'string' ) { + if ( !isString( str ) || str === '' ) { return null; } - - str = replace( str, ' ', '' ); - for ( i = 0; i < str.length; i++ ) { - if ( (str[i] === '+' || str[i] === '-') && i !== 0 && str[i - 1] !== 'e' ) { - parts.push( currentToken ); - currentToken = ''; - } else if ( i === str.length - 1 ) { - currentToken += str[ i ]; - parts.push( currentToken ); - } - currentToken += str[ i ]; - } - - for ( i = 0; i < parts.length; i++ ) { // Check for invalid parts... - part = parts[ i ]; - isImaginary = false; - - // Check for Iota on either sides: - if ( part[0] === 'i' ) { - part = part.slice( 1 ); - isImaginary = true; - } else if ( part[part.length - 1] === 'i' ) { - part = part.slice( 0, part.length - 1 ); - isImaginary = true; - } - - value = Number( part ); - if ( isNaN( value ) && part !== 'NaN' && part !== '+NaN' && part !== '-NaN' ) { - valid = false; - break; - } - - if ( isImaginary ) { - imaginaryParts.push( value ); - } - else { - realParts.push( value ); - } - } - - if ( !valid ) { + // OPTIMIZE: Consider implementing an alternative parser to improve performance over regular expression + match = str.match( regexp() ); // [ full_match, real_sign, real_value, imag_sign, imag_value, imag_suffix ] + if ( match === null ) { return null; } - - for ( i = 0; i < realParts.length; i++ ) { - re += realParts[i]; + // Real component: + if ( match[ 2 ] ) { + re = ( match[ 1 ] === '-' ) ? -1.0 : 1.0; + re *= parseFloat( match[ 2 ] ); + } else { + re = 0.0; } - for ( i = 0; i < imaginaryParts.length; i++ ) { - im += imaginaryParts[i]; + // Imaginary component: + if ( match[ 5 ] ) { + im = ( match[ 3 ] === '-' ) ? -1.0 : 1.0; + if ( match[ 4 ] ) { + im *= parseFloat( match[ 4 ] ); + } + } else { + im = 0.0; } - return { 're': re, 'im': im diff --git a/lib/node_modules/@stdlib/complex/base/parse/lib/regexp.js b/lib/node_modules/@stdlib/complex/base/parse/lib/regexp.js new file mode 100644 index 000000000000..2b208dca69b5 --- /dev/null +++ b/lib/node_modules/@stdlib/complex/base/parse/lib/regexp.js @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns a regular expression that matches and captures a complex number, supporting standard notation, scientific notation, Infinity, and NaN (e.g., "3+4i", "-1.5e10", "NaN-i"). +* +* Regular expression: `/^(?:\s*([+-]?)\s*((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)\s*(?=$|[+-]))?(?:\s*([+-]?)\s*((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)?\s*(i)\s*)?$/` +* +* - `^` +* - start of input +* +* - `(?:\s*([+-]?)\s*` +* - open an optional non-capturing group for the real part and capture an optional `+` or `-` literal for its sign, ignoring surrounding whitespace +* +* - `((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)` +* - capture the real magnitude, matching standard decimals, scientific notation, `Infinity`, or `NaN` +* +* - `\s*(?=$|[+-]))?` +* - match optional trailing whitespace, assert via positive lookahead that the real part is followed by either the end of input or the sign for the imaginary part, and close the real part group +* +* - `(?:\s*([+-]?)\s*` +* - open an optional non-capturing group for the imaginary part and capture an optional `+` or `-` literal for its sign, ignoring surrounding whitespace +* +* - `((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)?` +* - capture the optional imaginary magnitude, matching standard decimals, scientific notation, `Infinity`, or `NaN` +* +* - `\s*(i)\s*)?` +* - capture the literal character `i` for the imaginary unit, ignoring surrounding whitespace, and close the imaginary part group +* +* - `$` +* - end of input +* +* ## Notes +* +* - The regular expression has the following capture groups: +* +* 1. Sign of real part +* 2. Value of real part +* 3. Sign of imaginary part +* 4. Value of imaginary part +* 5. Imaginary suffix 'i' +* +* @private +* @returns {RegExp} regular expression +*/ +function regexp() { + return /^(?:\s*([+-]?)\s*((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)\s*(?=$|[+-]))?(?:\s*([+-]?)\s*((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)?\s*(i)\s*)?$/; +} + + +// EXPORTS // + +module.exports = regexp; diff --git a/lib/node_modules/@stdlib/complex/base/parse/test/test.js b/lib/node_modules/@stdlib/complex/base/parse/test/test.js index 7dcee4c10063..20881b8cb08e 100644 --- a/lib/node_modules/@stdlib/complex/base/parse/test/test.js +++ b/lib/node_modules/@stdlib/complex/base/parse/test/test.js @@ -141,6 +141,41 @@ tape( 'the function returns complex like object if provided valid complex string 're': Infinity, 'im': -Infinity } + }, + { + 'value': 'i', + 'expected': { + 're': 0, + 'im': 1 + } + }, + { + 'value': '1+i', + 'expected': { + 're': 1, + 'im': 1 + } + }, + { + 'value': '1-i', + 'expected': { + 're': 1, + 'im': -1 + } + }, + { + 'value': '.5 + 2.i', + 'expected': { + 're': 0.5, + 'im': 2 + } + }, + { + 'value': ' + 12.34e-5 - 54.32e1 i ', + 'expected': { + 're': 12.34e-5, + 'im': -54.32e1 + } } ]; @@ -161,7 +196,21 @@ tape( 'the function returns null if provided input is not a complex number strin Infinity, '55555555555555boop5', {}, - NaN + NaN, + '', + ' ', + ' ', + '+', + '-', + '2i + 1', + '12 3', + '.', + '1..2', + '1. 2', + '1.2e', + 'e5', + '1.2e3.5', + '1++2i' ]; for ( i = 0; i < values.length; i++ ) { diff --git a/lib/node_modules/@stdlib/complex/float32/parse/lib/main.js b/lib/node_modules/@stdlib/complex/float32/parse/lib/main.js index 86d48d8e6e69..95b3139baad5 100644 --- a/lib/node_modules/@stdlib/complex/float32/parse/lib/main.js +++ b/lib/node_modules/@stdlib/complex/float32/parse/lib/main.js @@ -21,28 +21,11 @@ // MODULES // var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var parse = require( '@stdlib/complex/base/parse' ); var Complex64 = require( '@stdlib/complex/float32/ctor' ); -var replace = require( '@stdlib/string/base/replace' ); var format = require( '@stdlib/string/format' ); -// FUNCTIONS // - -/** -* Matches a complex number string. -* -* @private -* @returns {RegExp} regular expression -* -* @example -* var re = regexp(); -* // returns /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/ -*/ -function regexp() { - return /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/; -} - - // MAIN // /** @@ -59,29 +42,16 @@ function regexp() { * // returns */ function parseComplex64( str ) { - var match; - var re; - var im = 0; + var v; if ( !isString( str ) ) { throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); } - - match = replace( str, /\s/g, '' ).match( regexp() ); - if ( !match ) { + v = parse( str ); + if ( v === null ) { throw new Error( format( 'invalid argument. Unable to parse input string as a complex number. Value: `%s`.', str ) ); } - - // Real part: - re = ( match[1] && !match[1].endsWith( 'i' ) ) ? parseFloat( match[1] ) : 0; - - // Imaginary part: - if ( match[4] ) { - im = ( ( match[3] === '-' ) ? -1 : 1 ) * parseFloat( replace( match[4], /i$/, '' ) ); - } else if ( match[1] && match[1].endsWith( 'i' ) ) { - im = parseFloat( replace( match[1], /i$/, '' ) ); - } - return new Complex64( re, im ); + return new Complex64( v.re, v.im ); } diff --git a/lib/node_modules/@stdlib/complex/float64/parse/lib/main.js b/lib/node_modules/@stdlib/complex/float64/parse/lib/main.js index 3bceef653762..41ed063a63bc 100644 --- a/lib/node_modules/@stdlib/complex/float64/parse/lib/main.js +++ b/lib/node_modules/@stdlib/complex/float64/parse/lib/main.js @@ -20,29 +20,12 @@ // MODULES // -var Complex128 = require( '@stdlib/complex/float64/ctor' ); var isString = require( '@stdlib/assert/is-string' ).isPrimitive; -var replace = require( '@stdlib/string/base/replace' ); +var parse = require( '@stdlib/complex/base/parse' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); var format = require( '@stdlib/string/format' ); -// FUNCTIONS // - -/** -* Matches a complex number string. -* -* @private -* @returns {RegExp} regular expression -* -* @example -* var re = regexp(); -* // returns /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/ -*/ -function regexp() { - return /^([-+]?(\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i?)?([-+])?((\d*\.?\d*(?:[eE][-+]?\d+)?|Infinity|NaN)i)?$/; -} - - // MAIN // /** @@ -59,29 +42,16 @@ function regexp() { * // returns */ function parseComplex128( str ) { - var match; - var re; - var im = 0; + var v; if ( !isString( str ) ) { throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); } - - match = replace( str, /\s/g, '' ).match( regexp() ); - if ( !match ) { + v = parse( str ); + if ( v === null ) { throw new Error( format( 'invalid argument. Unable to parse input string as a complex number. Value: `%s`.', str ) ); } - - // Real part: - re = ( match[1] && !match[1].endsWith( 'i' ) ) ? parseFloat( match[1] ) : 0; - - // Imaginary part: - if ( match[4] ) { - im = ( ( match[3] === '-' ) ? -1 : 1 ) * parseFloat( replace( match[4], /i$/, '' ) ); - } else if ( match[1] && match[1].endsWith( 'i' ) ) { - im = parseFloat( replace( match[1], /i$/, '' ) ); - } - return new Complex128( re, im ); + return new Complex128( v.re, v.im ); }