File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -1193,7 +1193,16 @@ export class EvaluationContext {
1193
1193
}
1194
1194
return list ( result . map ( int ) ) ;
1195
1195
}
1196
- // TODO: str2nr()
1196
+ case 'str2nr' : {
1197
+ const [ s , _base ] = getArgs ( 1 , 2 ) ;
1198
+ const base = _base ? toInt ( _base ) : 10
1199
+ if ( ! [ 2 , 8 , 10 , 16 ] . includes ( base ) ) {
1200
+ throw VimError . fromCode ( ErrorCode . InvalidArgument474 ) ;
1201
+ }
1202
+ // TODO: Skip prefixes like 0x
1203
+ const parsed = Number . parseInt ( toString ( s ! ) , base )
1204
+ return int ( isNaN ( parsed ) ? 0 : parsed ) ;
1205
+ }
1197
1206
// TODO: stridx()
1198
1207
case 'string' : {
1199
1208
const [ x ] = getArgs ( 1 ) ;
Original file line number Diff line number Diff line change @@ -733,6 +733,16 @@ suite('Vimscript expressions', () => {
733
733
exprTest ( 'str2list("á")' , { value : list ( [ int ( 97 ) , int ( 769 ) ] ) } ) ;
734
734
} ) ;
735
735
736
+ suite ( 'str2nr' , ( ) => {
737
+ exprTest ( 'str2nr("123")' , { value : int ( 123 ) } ) ;
738
+ exprTest ( 'str2nr("1001010110", 2)' , { value : int ( 598 ) } ) ;
739
+ exprTest ( 'str2nr("123", 8)' , { value : int ( 83 ) } ) ;
740
+ exprTest ( 'str2nr("123", 10)' , { value : int ( 123 ) } ) ;
741
+ exprTest ( 'str2nr("DEADBEEF", 16)' , { value : int ( 3735928559 ) } ) ;
742
+ exprTest ( 'str2nr("DEADBEEF", 10)' , { value : int ( 0 ) } ) ;
743
+ exprTest ( 'str2nr("DEADBEEF", 9)' , { error : ErrorCode . InvalidArgument474 } ) ;
744
+ } ) ;
745
+
736
746
suite ( 'string' , ( ) => {
737
747
exprTest ( 'string("")' , { value : str ( '' ) } ) ;
738
748
exprTest ( 'string(123)' , { value : str ( '123' ) } ) ;
You can’t perform that action at this time.
0 commit comments