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.
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.
11 use syntax::ast::{Visibility, Attribute, MetaItem, MetaItem_};
12 use syntax::codemap::{CodeMap, Span, BytePos};
14 use comment::FindUncommented;
19 pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
20 let snippet = codemap.span_to_snippet(original).unwrap();
22 original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1)
26 pub fn prev_char(s: &str, mut i: usize) -> usize {
27 if i == 0 { return 0; }
30 while !s.is_char_boundary(i) {
37 pub fn next_char(s: &str, mut i: usize) -> usize {
38 if i >= s.len() { return s.len(); }
40 while !s.is_char_boundary(i) {
47 pub fn make_indent(width: usize) -> String {
48 let mut indent = String::with_capacity(width);
56 pub fn format_visibility(vis: Visibility) -> &'static str {
58 Visibility::Public => "pub ",
59 Visibility::Inherited => ""
63 fn is_skip(meta_item: &MetaItem) -> bool {
64 match meta_item.node {
65 MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,
71 pub fn contains_skip(attrs: &[Attribute]) -> bool {
72 attrs.iter().any(|a| is_skip(&a.node.value))
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);
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);
102 // Macro for deriving implementations of Decodable for enums
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());
111 stringify!($x) => Ok($e::$x),
113 _ => Err(d.error("Bad variant")),
120 // Same as try!, but for Option
122 macro_rules! try_opt {
123 ($expr:expr) => (match $expr {
125 None => { return None; }
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));