1- use std:: path:: PathBuf ;
2-
31use logos:: { Lexer , Logos } ;
42use regex:: Regex ;
53
@@ -36,57 +34,53 @@ impl Opcode {
3634 let mut remainder = String :: new ( ) ; // the remainder opcode name after the current parameter
3735
3836 // Tries to find numeric parameters embedded in the name
39- let lex_numbers = OpcodeParameter :: lexer ( & name) ;
37+ let lex_numbers = OpcodeParameter :: lexer ( name) ;
4038 for ( n, span) in lex_numbers. spanned ( ) {
41- match & n {
42- // If a parameter is found
43- OpcodeParameter :: Parameter ( p) => {
44- // constructs the new name of the opcode,
45- // with the numeric parameters being substituted by
46- // the N, X, Y chars, in that order.
47-
48- let split_idx = span. start ; // the index of current parameter in original name
39+ // If a parameter is found
40+ if let OpcodeParameter :: Parameter ( p) = & n {
41+ // constructs the new name of the opcode,
42+ // with the numeric parameters being substituted by
43+ // the N, X, Y chars, in that order.
4944
50- let ( first, last) = name. split_at ( split_idx) ;
51- remainder = last[ span. end - span. start ..] . to_string ( ) ;
45+ let split_idx = span. start ; // the index of current parameter in original name
5246
53- // first handle the special case of 4 opcodes with an "NN" parameter:
54- // (varNN_mod, varNN_onccX, varNN_curveccX, varNN_target)
55- if par_num == 0 && Regex :: new ( r"^var" ) . unwrap ( ) . is_match ( & name) {
56- new_name += & format ! ( "{}NN" , & first[ previous_span_end..] ) ;
47+ let ( first, last) = name. split_at ( split_idx) ;
48+ remainder = last[ span. end - span. start ..] . to_string ( ) ;
5749
58- // then handle the rest of the cases
59- } else {
60- new_name +=
61- & format ! ( "{}{}" , & first[ previous_span_end..] , par_char[ par_num] ) ;
62- }
50+ // first handle the special case of 4 opcodes with an "NN" parameter:
51+ // (varNN_mod, varNN_onccX, varNN_curveccX, varNN_target)
52+ if par_num == 0 && Regex :: new ( r"^var" ) . unwrap ( ) . is_match ( name) {
53+ new_name += & format ! ( "{}NN" , & first[ previous_span_end..] ) ;
6354
64- //println!("{} ({}) remainder: {}", &new_name, previous_span_end, remainder); // DEBUG
65-
66- // TODO: check for numeric boundaries
67- //
68- // TODO NOTE
69- // 1. all opcodes ending in ccN has N = 0..=127 (or ccX when N is already used)
70- // (except some sfz2 and aria extensions, see:
71- // https://sfzformat.com/extensions/midi_ccs
72- //
73- // 2. each of these has N = 1..=3 (and X = 0..=127)
74- // eqN_bw
75- // eqN_bwccX
76- // eqN_freq
77- // eqN_freqccX
78- // eqN_vel2freq
79- // eqN_gain
80- // eqN_gainccX
81- // eqN_vel2gain
82-
83- // Stores the numeric parameter
84- params. push ( * p as u8 ) ;
85-
86- par_num += 1 ;
87- previous_span_end = span. end ;
55+ // then handle the rest of the cases
56+ } else {
57+ new_name += & format ! ( "{}{}" , & first[ previous_span_end..] , par_char[ par_num] ) ;
8858 }
89- _ => ( ) ,
59+
60+ //println!("{} ({}) remainder: {}", &new_name, previous_span_end, remainder); // DEBUG
61+
62+ // TODO: check for numeric boundaries
63+ //
64+ // TODO NOTE
65+ // 1. all opcodes ending in ccN has N = 0..=127 (or ccX when N is already used)
66+ // (except some sfz2 and aria extensions, see:
67+ // https://sfzformat.com/extensions/midi_ccs
68+ //
69+ // 2. each of these has N = 1..=3 (and X = 0..=127)
70+ // eqN_bw
71+ // eqN_bwccX
72+ // eqN_freq
73+ // eqN_freqccX
74+ // eqN_vel2freq
75+ // eqN_gain
76+ // eqN_gainccX
77+ // eqN_vel2gain
78+
79+ // Stores the numeric parameter
80+ params. push ( * p as u8 ) ;
81+
82+ par_num += 1 ;
83+ previous_span_end = span. end ;
9084 }
9185 }
9286
@@ -95,7 +89,7 @@ impl Opcode {
9589 new_name = name. to_string ( ) ;
9690
9791 // In case there's a remainder after the last parameter, append it
98- } else if remainder. len ( ) > 0 {
92+ } else if ! remainder. is_empty ( ) {
9993 new_name += & remainder;
10094 }
10195
@@ -140,7 +134,7 @@ impl Opcode {
140134
141135 let slice = lex. slice ( ) ;
142136
143- let kv: Vec < & str > = slice. splitn ( 2 , "=" ) . collect ( ) ;
137+ let kv: Vec < & str > = slice. splitn ( 2 , '=' ) . collect ( ) ;
144138 let ( opcode, value) = ( kv[ 0 ] , kv[ 1 ] ) ;
145139 let value = value. trim ( ) ; // remove possible remaining CRLF chars
146140
@@ -206,9 +200,7 @@ impl Opcode {
206200 utils:: check_u16_between ( value, 0 , 9600 ) . map ( Opcode :: pitch_random)
207201 }
208202 ( "rt_decay" , _) => utils:: check_f32_between ( value, 0. , 200. ) . map ( Opcode :: rt_decay) ,
209- ( "sample" , _) => Some ( Opcode :: sample ( PathBuf :: from ( utils:: fix_path_separators (
210- value,
211- ) ) ) ) ,
203+ ( "sample" , _) => Some ( Opcode :: sample ( utils:: fix_path_separators ( value) ) ) ,
212204 ( "seq_lenght" , _) => utils:: check_u8_between ( value, 1 , 100 ) . map ( Opcode :: seq_length) ,
213205 ( "seq_position" , _) => utils:: check_u8_between ( value, 1 , 100 ) . map ( Opcode :: seq_position) ,
214206 ( "trigger" , _) => trigger:: from_str ( value) . map ( Opcode :: trigger) ,
@@ -298,6 +290,7 @@ pub(crate) enum OpcodeParameter {
298290mod tests_opcodes {
299291 use super :: * ;
300292 use logos:: Logos ;
293+ use std:: path:: PathBuf ;
301294
302295 #[ test]
303296 fn test_opcode_ampeg_attack ( ) {
0 commit comments