util.rs 895 B

12345678910111213141516171819202122232425262728293031323334
  1. use lib_ot::core::{NEW_LINE, WHITESPACE};
  2. use std::sync::atomic::{AtomicI64, Ordering::SeqCst};
  3. #[inline]
  4. pub fn find_newline(s: &str) -> Option<usize> { s.find(NEW_LINE) }
  5. #[inline]
  6. pub fn is_newline(s: &str) -> bool { s == NEW_LINE }
  7. #[inline]
  8. pub fn is_whitespace(s: &str) -> bool { s == WHITESPACE }
  9. #[inline]
  10. pub fn contain_newline(s: &str) -> bool { s.contains(NEW_LINE) }
  11. #[inline]
  12. pub fn md5<T: AsRef<[u8]>>(data: T) -> String {
  13. let md5 = format!("{:x}", md5::compute(data));
  14. md5
  15. }
  16. #[derive(Debug)]
  17. pub struct RevIdCounter(pub AtomicI64);
  18. impl RevIdCounter {
  19. pub fn new(n: i64) -> Self { Self(AtomicI64::new(n)) }
  20. pub fn next(&self) -> i64 {
  21. let _ = self.0.fetch_add(1, SeqCst);
  22. self.value()
  23. }
  24. pub fn value(&self) -> i64 { self.0.load(SeqCst) }
  25. pub fn set(&self, n: i64) { let _ = self.0.fetch_update(SeqCst, SeqCst, |_| Some(n)); }
  26. }