Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 24 additions & 55 deletions lib/node_modules/@stdlib/complex/base/parse/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

// MODULES //

var replace = require( '@stdlib/string/replace' );
var Number = require( '@stdlib/number/ctor' );
var regexp = require( './regexp.js' );


// MAIN //
Expand All @@ -46,70 +46,39 @@
* // 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 ( typeof str !== 'string' || 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;
}
// OPTIMIZE: Consider implementing an alternative parser to improve performance over regular expression

Check warning on line 57 in lib/node_modules/@stdlib/complex/base/parse/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'optimize' comment: 'OPTIMIZE: Consider implementing an...'
match = str.match( regexp() ); // [ full_match, real_sign, real_value, imag_sign, imag_value, imag_suffix ]

if ( isImaginary ) {
imaginaryParts.push( value );
}
else {
realParts.push( value );
}
if ( match === null ) {
return null;
}

if ( !valid ) {
return null;
// Real component:
if ( match[ 2 ] ) {
re = ( match[ 1 ] === '-' ) ? -1 : 1;
re *= Number.parseFloat( match[ 2 ] );
}
else {
re = 0;
}

for ( i = 0; i < realParts.length; i++ ) {
re += realParts[i];
// Imaginary component:
if ( match[ 5 ] ) {
im = ( match[ 3 ] === '-' ) ? -1 : 1;
if ( match[ 4 ] ) {
im *= Number.parseFloat( match[ 4 ] );
}
}
for ( i = 0; i < imaginaryParts.length; i++ ) {
im += imaginaryParts[i];
else {
im = 0;
}

return {
Expand Down
73 changes: 73 additions & 0 deletions lib/node_modules/@stdlib/complex/base/parse/lib/regexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @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 //

/**
* Return 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
* @constant
* @returns {RegExp} regular expression
* @default /^(?:\s*([+-]?)\s*((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)\s*(?=$|[+-]))?(?:\s*([+-]?)\s*((?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|Infinity|NaN)?\s*(i)\s*)?$/
*/
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;
51 changes: 50 additions & 1 deletion lib/node_modules/@stdlib/complex/base/parse/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
];

Expand All @@ -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++ ) {
Expand Down
38 changes: 5 additions & 33 deletions lib/node_modules/@stdlib/complex/float32/parse/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 //

/**
Expand All @@ -59,29 +42,18 @@ function regexp() {
* // returns <Complex64>
*/
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 );
}


Expand Down
40 changes: 6 additions & 34 deletions lib/node_modules/@stdlib/complex/float64/parse/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 //

/**
Expand All @@ -59,29 +42,18 @@ function regexp() {
* // returns <Complex128>
*/
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 );
}


Expand Down