File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change 5
5
*/
6
6
7
7
import Operation from "../Operation.mjs" ;
8
+ import Utils from "../Utils.mjs" ;
8
9
9
10
/**
10
11
* Reverse operation
@@ -26,7 +27,8 @@ class Reverse extends Operation {
26
27
{
27
28
"name" : "By" ,
28
29
"type" : "option" ,
29
- "value" : [ "Character" , "Line" ]
30
+ "value" : [ "Byte" , "Character" , "Line" ] ,
31
+ "defaultIndex" : 1
30
32
}
31
33
] ;
32
34
}
@@ -57,6 +59,24 @@ class Reverse extends Operation {
57
59
result . push ( 0x0a ) ;
58
60
}
59
61
return result . slice ( 0 , input . length ) ;
62
+ } else if ( args [ 0 ] === "Character" ) {
63
+ const inputString = Utils . byteArrayToUtf8 ( input ) ;
64
+ let result = "" ;
65
+ for ( let i = inputString . length - 1 ; i >= 0 ; i -- ) {
66
+ const c = inputString . charCodeAt ( i ) ;
67
+ if ( i > 0 && 0xdc00 <= c && c <= 0xdfff ) {
68
+ const c2 = inputString . charCodeAt ( i - 1 ) ;
69
+ if ( 0xd800 <= c2 && c2 <= 0xdbff ) {
70
+ // surrogates
71
+ result += inputString . charAt ( i - 1 ) ;
72
+ result += inputString . charAt ( i ) ;
73
+ i -- ;
74
+ continue ;
75
+ }
76
+ }
77
+ result += inputString . charAt ( i ) ;
78
+ }
79
+ return Utils . strToUtf8ByteArray ( result ) ;
60
80
} else {
61
81
return input . reverse ( ) ;
62
82
}
You can’t perform that action at this time.
0 commit comments