]> git.lizzy.rs Git - rust.git/blob - src/utils.rs
Format comments in struct literals
[rust.git] / src / utils.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use syntax::ast::{Visibility, Attribute, MetaItem, MetaItem_};
12 use syntax::codemap::{CodeMap, Span, BytePos};
13
14 use comment::FindUncommented;
15
16 use SKIP_ANNOTATION;
17
18 #[inline]
19 pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
20     let snippet = codemap.span_to_snippet(original).unwrap();
21
22     original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1)
23 }
24
25 #[inline]
26 pub fn prev_char(s: &str, mut i: usize) -> usize {
27     if i == 0 { return 0; }
28
29     i -= 1;
30     while !s.is_char_boundary(i) {
31         i -= 1;
32     }
33     i
34 }
35
36 #[inline]
37 pub fn next_char(s: &str, mut i: usize) -> usize {
38     if i >= s.len() { return s.len(); }
39
40     while !s.is_char_boundary(i) {
41         i += 1;
42     }
43     i
44 }
45
46 #[inline]
47 pub fn make_indent(width: usize) -> String {
48     let mut indent = String::with_capacity(width);
49     for _ in 0..width {
50         indent.push(' ')
51     }
52     indent
53 }
54
55 #[inline]
56 pub fn format_visibility(vis: Visibility) -> &'static str {
57     match vis {
58         Visibility::Public => "pub ",
59         Visibility::Inherited => ""
60     }
61 }
62
63 fn is_skip(meta_item: &MetaItem) -> bool {
64     match meta_item.node {
65         MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,
66         _ => false,
67     }
68 }
69
70 #[inline]
71 pub fn contains_skip(attrs: &[Attribute]) -> bool {
72     attrs.iter().any(|a| is_skip(&a.node.value))
73 }
74
75 #[inline]
76 #[cfg(target_pointer_width="64")]
77 // Based on the trick layed out at
78 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
79 pub fn round_up_to_power_of_two(mut x: usize) -> usize {
80     x = x.wrapping_sub(1);
81     x |= x >> 1;
82     x |= x >> 2;
83     x |= x >> 4;
84     x |= x >> 8;
85     x |= x >> 16;
86     x |= x >> 32;
87     x.wrapping_add(1)
88 }
89
90 #[inline]
91 #[cfg(target_pointer_width="32")]
92 pub fn round_up_to_power_of_two(mut x: usize) -> usize {
93     x = x.wrapping_sub(1);
94     x |= x >> 1;
95     x |= x >> 2;
96     x |= x >> 4;
97     x |= x >> 8;
98     x |= x >> 16;
99     x.wrapping_add(1)
100 }
101
102 // Macro for deriving implementations of Decodable for enums
103 #[macro_export]
104 macro_rules! impl_enum_decodable {
105     ( $e:ident, $( $x:ident ),* ) => {
106         impl ::rustc_serialize::Decodable for $e {
107             fn decode<D: ::rustc_serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
108                 let s = try!(d.read_str());
109                 match &*s {
110                     $(
111                         stringify!($x) => Ok($e::$x),
112                     )*
113                     _ => Err(d.error("Bad variant")),
114                 }
115             }
116         }
117     };
118 }
119
120 // Same as try!, but for Option
121 #[macro_export]
122 macro_rules! try_opt {
123     ($expr:expr) => (match $expr {
124         Some(val) => val,
125         None => { return None; }
126     })
127 }
128
129 #[test]
130 fn power_rounding() {
131     assert_eq!(0, round_up_to_power_of_two(0));
132     assert_eq!(1, round_up_to_power_of_two(1));
133     assert_eq!(64, round_up_to_power_of_two(33));
134     assert_eq!(256, round_up_to_power_of_two(256));
135 }