GithubHelp home page GithubHelp logo

objectivecharm / indexed_string Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 14 KB

A naive implementation for rust's string to support index, works with unicode in O(n) time.

License: MIT License

Rust 100.00%

indexed_string's Introduction

⚠️⚠️This crate is just a practice work with not taking grapheme cluster into consideration and will have undefined behaviour when try to convert from string has grapheme clusters. Sadly,I have no idea how to recognize boundary of grapheme clusters

Indexing UTF-8 String

Rust's stdlib does not support index and access string with vec-like subscript syntax. Slicing string could also be "dangerous" for it will panic and crash if you achieve the middle of single utf-8 char.

"You should use ranges to create string slices with caution, because doing so can crash your program"

So I try to make this small and simple library to make things simpler and less tiring.

The library offers wrapper type IndexedString based on vec witch supports indexing, iterating and modifying string. And it can convert from and to string and string slice simply and safely like vec.

Installation

Simply add dependency to cargo.toml

[dependencies]
#... other
indexed_string = "0.1.0"

Usage

/// import mod to scope
use indexed_string::indexed_string::IndexedString;

let str = "Stand with Ukraine";

/// convert &str to IndexedString
let indexed_string = IndexedString::from(str);

/// immutably access string
assert_eq!(indexed_string[1], "t");
/// mutably access string 
indexed_string[0] = "s";

/// Get modified new string
println!("{}", indexed_string.to_string());

Just naive implementation and experimental product

As its description, it is just a naive implementation(I am not familiar with rust)

  1. To detect utf-8 char and variation selector I write too much nest if statements, which make the code hard to read. Maybe using macro instead my helps?
  2. I am not familiar with bit operations which make code worse.
  3. I am not familiar with lifetime, so I simply use Box to encapsulate String every single character, which may make the methods of wrapper more ugly and inefficient?

But it is tested and works :)

Contribute

Feel free to fork it and open pull request for refactoring, bug fix and so on.

License

MIT for detail please visit the website

indexed_string's People

Contributors

objectivecharm avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.