@@ -413,27 +413,14 @@ impl<'a> Id<'a> {
413
413
/// quotes, ...) will return an empty `Err` value.
414
414
pub fn new < Name : IntoCow < ' a , str > > ( name : Name ) -> Result < Id < ' a > , ( ) > {
415
415
let name = name. into_cow ( ) ;
416
- {
417
- let mut chars = name. chars ( ) ;
418
- match chars. next ( ) {
419
- Some ( c) if is_letter_or_underscore ( c) => { }
420
- _ => return Err ( ( ) ) ,
421
- }
422
- if !chars. all ( is_constituent) {
423
- return Err ( ( ) ) ;
424
- }
425
- }
426
- return Ok ( Id { name : name } ) ;
427
-
428
- fn is_letter_or_underscore ( c : char ) -> bool {
429
- in_range ( 'a' , c, 'z' ) || in_range ( 'A' , c, 'Z' ) || c == '_'
430
- }
431
- fn is_constituent ( c : char ) -> bool {
432
- is_letter_or_underscore ( c) || in_range ( '0' , c, '9' )
416
+ match name. chars ( ) . next ( ) {
417
+ Some ( c) if c. is_ascii_alphabetic ( ) || c == '_' => { }
418
+ _ => return Err ( ( ) ) ,
433
419
}
434
- fn in_range ( low : char , c : char , high : char ) -> bool {
435
- low as usize <= c as usize && c as usize <= high as usize
420
+ if !name . chars ( ) . all ( |c| c . is_ascii_alphanumeric ( ) || c == '_' ) {
421
+ return Err ( ( ) ) ;
436
422
}
423
+ return Ok ( Id { name : name } ) ;
437
424
}
438
425
439
426
pub fn as_slice ( & ' a self ) -> & ' a str {
@@ -484,8 +471,7 @@ pub trait Labeller<'a> {
484
471
/// Maps `e` to a label that will be used in the rendered output.
485
472
/// The label need not be unique, and may be the empty string; the
486
473
/// default is in fact the empty string.
487
- fn edge_label ( & ' a self , e : & Self :: Edge ) -> LabelText < ' a > {
488
- let _ignored = e;
474
+ fn edge_label ( & ' a self , _e : & Self :: Edge ) -> LabelText < ' a > {
489
475
LabelStr ( "" . into_cow ( ) )
490
476
}
491
477
@@ -655,79 +641,58 @@ pub fn render_opts<'a, N, E, G, W>(g: &'a G,
655
641
G : Labeller < ' a , Node =N , Edge =E > + GraphWalk < ' a , Node =N , Edge =E > ,
656
642
W : Write
657
643
{
658
- fn writeln < W : Write > ( w : & mut W , arg : & [ & str ] ) -> io:: Result < ( ) > {
659
- for & s in arg {
660
- w. write_all ( s. as_bytes ( ) ) ?;
661
- }
662
- write ! ( w, "\n " )
663
- }
664
-
665
- fn indent < W : Write > ( w : & mut W ) -> io:: Result < ( ) > {
666
- w. write_all ( b" " )
667
- }
668
-
669
- writeln ( w, & [ "digraph " , g. graph_id ( ) . as_slice ( ) , " {" ] ) ?;
644
+ writeln ! ( w, "digraph {} {{" , g. graph_id( ) . as_slice( ) ) ?;
670
645
for n in g. nodes ( ) . iter ( ) {
671
- indent ( w ) ?;
646
+ write ! ( w , " " ) ?;
672
647
let id = g. node_id ( n) ;
673
648
674
649
let escaped = & g. node_label ( n) . to_dot_string ( ) ;
675
- let shape;
676
650
677
- let mut text = vec ! [ id. as_slice( ) ] ;
651
+ let mut text = Vec :: new ( ) ;
652
+ write ! ( text, "{}" , id. as_slice( ) ) . unwrap ( ) ;
678
653
679
654
if !options. contains ( & RenderOption :: NoNodeLabels ) {
680
- text. push ( "[label=" ) ;
681
- text. push ( escaped) ;
682
- text. push ( "]" ) ;
655
+ write ! ( text, "[label={}]" , escaped) . unwrap ( ) ;
683
656
}
684
657
685
658
let style = g. node_style ( n) ;
686
659
if !options. contains ( & RenderOption :: NoNodeStyles ) && style != Style :: None {
687
- text. push ( "[style=\" " ) ;
688
- text. push ( style. as_slice ( ) ) ;
689
- text. push ( "\" ]" ) ;
660
+ write ! ( text, "[style=\" {}\" ]" , style. as_slice( ) ) . unwrap ( ) ;
690
661
}
691
662
692
663
if let Some ( s) = g. node_shape ( n) {
693
- shape = s. to_dot_string ( ) ;
694
- text. push ( "[shape=" ) ;
695
- text. push ( & shape) ;
696
- text. push ( "]" ) ;
664
+ write ! ( text, "[shape={}]" , & s. to_dot_string( ) ) . unwrap ( ) ;
697
665
}
698
666
699
- text . push ( ";" ) ;
700
- writeln ( w , & text) ?;
667
+ writeln ! ( text , ";" ) . unwrap ( ) ;
668
+ w . write_all ( & text[ .. ] ) ?;
701
669
}
702
670
703
671
for e in g. edges ( ) . iter ( ) {
704
672
let escaped_label = & g. edge_label ( e) . to_dot_string ( ) ;
705
- indent ( w ) ?;
673
+ write ! ( w , " " ) ?;
706
674
let source = g. source ( e) ;
707
675
let target = g. target ( e) ;
708
676
let source_id = g. node_id ( & source) ;
709
677
let target_id = g. node_id ( & target) ;
710
678
711
- let mut text = vec ! [ source_id. as_slice( ) , " -> " , target_id. as_slice( ) ] ;
679
+ let mut text = Vec :: new ( ) ;
680
+ write ! ( text, "{} -> {}" , source_id. as_slice( ) , target_id. as_slice( ) ) . unwrap ( ) ;
712
681
713
682
if !options. contains ( & RenderOption :: NoEdgeLabels ) {
714
- text. push ( "[label=" ) ;
715
- text. push ( escaped_label) ;
716
- text. push ( "]" ) ;
683
+ write ! ( text, "[label={}]" , escaped_label) . unwrap ( ) ;
717
684
}
718
685
719
686
let style = g. edge_style ( e) ;
720
687
if !options. contains ( & RenderOption :: NoEdgeStyles ) && style != Style :: None {
721
- text. push ( "[style=\" " ) ;
722
- text. push ( style. as_slice ( ) ) ;
723
- text. push ( "\" ]" ) ;
688
+ write ! ( text, "[style=\" {}\" ]" , style. as_slice( ) ) . unwrap ( ) ;
724
689
}
725
690
726
- text . push ( ";" ) ;
727
- writeln ( w , & text) ?;
691
+ writeln ! ( text , ";" ) . unwrap ( ) ;
692
+ w . write_all ( & text[ .. ] ) ?;
728
693
}
729
694
730
- writeln ( w, & [ "}" ] )
695
+ writeln ! ( w, "}}" )
731
696
}
732
697
733
698
pub trait IntoCow < ' a , B : ?Sized > where B : ToOwned {
0 commit comments