util.rs 990 B

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