@@ -203,7 +203,7 @@ where
203203 }
204204}
205205
206- fn parse_tr < ' a > ( s : & ' a str ) -> Result < Tree < ' a > , Error > {
206+ fn parse_tr ( s : & str ) -> Result < Tree , Error > {
207207 for ch in s. bytes ( ) {
208208 if ch > 0x7f {
209209 return Err ( Error :: Unprintable ( ch) ) ;
@@ -224,8 +224,8 @@ fn parse_tr<'a>(s: &'a str) -> Result<Tree<'a>, Error> {
224224 args : vec ! [ key_path] ,
225225 } ) ;
226226 }
227- let ( key , script ) = rest
228- . split_once ( ',' )
227+ // use str::split_once() method to refactor this when compiler version bumps up
228+ let ( key , script ) = split_once ( rest , ',' )
229229 . ok_or_else ( || Error :: BadDescriptor ( "invalid taproot descriptor" . to_string ( ) ) ) ?;
230230 let key_path = Tree {
231231 name : key,
@@ -237,7 +237,7 @@ fn parse_tr<'a>(s: &'a str) -> Result<Tree<'a>, Error> {
237237 args : vec ! [ key_path] ,
238238 } ) ;
239239 }
240- let ( script_path, rest) = expression:: Tree :: from_slice_helper_curly ( script, 0 ) ?;
240+ let ( script_path, rest) = expression:: Tree :: from_slice_helper_curly ( script, 1 ) ?;
241241 if rest. is_empty ( ) {
242242 Ok ( Tree {
243243 name : "tr" ,
@@ -250,3 +250,22 @@ fn parse_tr<'a>(s: &'a str) -> Result<Tree<'a>, Error> {
250250 Err ( Error :: Unexpected ( "invalid taproot descriptor" . to_string ( ) ) )
251251 } ;
252252}
253+
254+ fn split_once ( inp : & str , delim : char ) -> Option < ( & str , & str ) > {
255+ return if inp. len ( ) == 0 {
256+ None
257+ } else {
258+ let mut found = 0 ;
259+ for ( idx, ch) in inp. chars ( ) . enumerate ( ) {
260+ if ch == delim {
261+ found = idx;
262+ break ;
263+ }
264+ }
265+ if found == inp. len ( ) - 1 {
266+ Some ( ( & inp[ ..] , & "" ) )
267+ } else {
268+ Some ( ( & inp[ ..found] , & inp[ found + 1 ..] ) )
269+ }
270+ } ;
271+ }
0 commit comments