|
1 | 1 | //! A higher level attributes based on TokenTree, with also some shortcuts. |
2 | | -use std::{fmt, ops}; |
| 2 | +use std::{borrow::Cow,fmt, ops}; |
3 | 3 |
|
4 | 4 | use base_db::CrateId; |
5 | 5 | use cfg::CfgExpr; |
@@ -297,6 +297,20 @@ impl Attr { |
297 | 297 | } |
298 | 298 | } |
299 | 299 |
|
| 300 | +pubfnstring_value_unescape(&self) -> Option<Cow<'_,str>>{ |
| 301 | +matchself.input.as_deref()? { |
| 302 | +AttrInput::Literal(it) => match it.text.strip_prefix('r'){ |
| 303 | +Some(it) => { |
| 304 | + it.trim_matches('#').strip_prefix('"')?.strip_suffix('"').map(Cow::Borrowed) |
| 305 | +} |
| 306 | +None => { |
| 307 | + it.text.strip_prefix('"')?.strip_suffix('"').and_then(unescape).map(Cow::Owned) |
| 308 | +} |
| 309 | +}, |
| 310 | + _ => None, |
| 311 | +} |
| 312 | +} |
| 313 | + |
300 | 314 | /// #[path(ident)] |
301 | 315 | pubfnsingle_ident_value(&self) -> Option<&tt::Ident>{ |
302 | 316 | matchself.input.as_deref()? { |
@@ -346,6 +360,39 @@ impl Attr { |
346 | 360 | } |
347 | 361 | } |
348 | 362 |
|
| 363 | +fnunescape(s:&str) -> Option<String>{ |
| 364 | +letmut res = String::with_capacity(s.len()); |
| 365 | +letmut chars = s.chars(); |
| 366 | + |
| 367 | +whileletSome(c) = chars.next(){ |
| 368 | +if c == '\\'{ |
| 369 | +match chars.next()? { |
| 370 | +'n' => res.push('\n'), |
| 371 | +'r' => res.push('\r'), |
| 372 | +'t' => res.push('\t'), |
| 373 | +'\\' => res.push('\\'), |
| 374 | +'\'' => res.push('\''), |
| 375 | +'"' => res.push('"'), |
| 376 | +'0' => res.push('\0'), |
| 377 | +'x' => { |
| 378 | +let hex = chars.by_ref().take(2).collect::<String>(); |
| 379 | +let c = u8::from_str_radix(&hex,16).ok()?; |
| 380 | + res.push(c aschar); |
| 381 | +} |
| 382 | +'u' => { |
| 383 | +let hex = chars.by_ref().take(4).collect::<String>(); |
| 384 | +let c = u32::from_str_radix(&hex,16).ok()?; |
| 385 | + res.push(char::from_u32(c)?); |
| 386 | +} |
| 387 | + _ => returnNone, |
| 388 | +} |
| 389 | +}else{ |
| 390 | + res.push(c); |
| 391 | +} |
| 392 | +} |
| 393 | +Some(res) |
| 394 | +} |
| 395 | + |
349 | 396 | pubfncollect_attrs( |
350 | 397 | owner:&dyn ast::HasAttrs, |
351 | 398 | ) -> implIterator<Item = (AttrId,Either<ast::Attr, ast::Comment>)>{ |
|
0 commit comments