dequote 0.9.0

Remove nested quotes around text
Documentation
/*
dequote - Simple no-std text processing library
Written by Radim Kolar <hsn@sendmail.cz> 2024
https://gitlab.com/hsn10/dequote

This is free and unencumbered software released into the public domain.
For more information, please refer to <https://unlicense.org/>.

CC0: This work has been marked as dedicated to the public domain.
For more information, please refer to <https://creativecommons.org/public-domain/cc0/>

SPDX-License-Identifier: Unlicense OR CC0-1.0
*/

#![forbid(unsafe_code)]
#![forbid(missing_docs)]
#![forbid(non_fmt_panics)]

//! Dequote is small no-std text processing library.
//!
//! Library have two families of functions: trim and dequote.
//! 1. trim, ltrim, rtrim removes whitespace around text and
//!    returns a str slice.
//! 1. dequote removes nested quotes around text and returns a str slice.
//!
//! This is free and unencumbered software released into the public domain.
//! For more information, please refer to <https://unlicense.org/>.


// disable std library if we are not running tests.
#![cfg_attr(not(test), no_std)]

//     T   R   I   M

/**
  Remove leading whitespace
*/
pub fn ltrim<'a>(input: &'a impl AsRef<str>) -> &'a str {
    input.as_ref().trim_start()
}

/**
  Remove trailing whitespace
*/
pub fn rtrim<'a>(input: &'a impl AsRef<str>) -> &'a str {
    input.as_ref().trim_end()
}

/**
  Remove whitespace around text
*/
pub fn trim<'a>(input: &'a impl AsRef<str>) -> &'a str {
    input.as_ref().trim()
}


//     D   E   Q   U   O   T   E

/**
   Characters considered to be quotes */
pub const QUOTES: [ char; 3 ] = [ '"', '\'', '`' ];

/**
   Remove nested quotes around text
*/
pub fn dequote<'a>(input: &'a impl AsRef<str>) -> &'a str {
   let iref = input.as_ref();
   /* number of leading quotes detected */
   let mut nqts: usize = 0;
   for c in iref.chars() {
      if QUOTES.contains(&c) {
         nqts += 1;
      } else {
         break;
      }
   }
   if nqts == 0 {
      iref
   } else {
     /* real number of quote pairs */
     let mut strip = 0;
     for i in 0..nqts {
        if iref.chars().nth_back(i) == iref.chars().nth(i) {
           strip += 1;
        } else {
          break;
        }
     }
     if strip == 0 {
        iref
     } else {
        &iref[(strip)..(iref.len() - strip)]
     }     
   }
}


#[cfg(test)]
mod trim_tests;

#[cfg(test)]
mod dequote_tests;