strings.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. // See LICENSE for licensing information */
  3. //! Utilities for working with static strings.
  4. use std::ffi::CStr;
  5. /// A byte-array containing a single NUL byte (`b"\0"`).
  6. pub const NUL_BYTE: &'static [u8] = b"\0";
  7. /// Determine if a byte slice is a C-like string.
  8. ///
  9. /// These checks guarantee that:
  10. ///
  11. /// 1. there are no intermediate NUL bytes
  12. /// 2. the last byte *is* a NUL byte
  13. ///
  14. /// # Warning
  15. ///
  16. /// This function does _not_ guarantee that the bytes represent any valid
  17. /// encoding such as ASCII or UTF-8.
  18. ///
  19. /// # Examples
  20. ///
  21. /// ```
  22. /// # use tor_util::strings::byte_slice_is_c_like;
  23. /// #
  24. /// let bytes: &[u8] = b"foo bar baz";
  25. ///
  26. /// assert!(byte_slice_is_c_like(&bytes) == false);
  27. ///
  28. /// let bytes: &[u8] = b"foo\0bar baz";
  29. ///
  30. /// assert!(byte_slice_is_c_like(&bytes) == false);
  31. ///
  32. /// let bytes: &[u8] = b"foo bar baz\0";
  33. ///
  34. /// assert!(byte_slice_is_c_like(&bytes) == true);
  35. /// ```
  36. pub fn byte_slice_is_c_like(bytes: &[u8]) -> bool {
  37. if !bytes[..bytes.len() - 1].contains(&0x00) && bytes[bytes.len() - 1] == 0x00 {
  38. return true;
  39. }
  40. false
  41. }
  42. /// Get a static `CStr` containing a single `NUL_BYTE`.
  43. ///
  44. /// # Examples
  45. ///
  46. /// When used as follows in a Rust FFI function, which could be called
  47. /// from C:
  48. ///
  49. /// ```
  50. /// # extern crate libc;
  51. /// # extern crate tor_util;
  52. /// #
  53. /// # use tor_util::strings::empty_static_cstr;
  54. /// use libc::c_char;
  55. /// use std::ffi::CStr;
  56. ///
  57. /// pub extern "C" fn give_c_code_an_empty_static_string() -> *const c_char {
  58. /// let empty: &'static CStr = empty_static_cstr();
  59. ///
  60. /// empty.as_ptr()
  61. /// }
  62. ///
  63. /// # fn main() {
  64. /// # give_c_code_an_empty_static_string();
  65. /// # }
  66. /// ```
  67. ///
  68. /// This equates to an "empty" `const char*` static string in C.
  69. pub fn empty_static_cstr() -> &'static CStr {
  70. let empty: &'static CStr;
  71. unsafe {
  72. empty = CStr::from_bytes_with_nul_unchecked(NUL_BYTE);
  73. }
  74. empty
  75. }