Giter Site home page Giter Site logo

c-utf8-rs's Introduction

C-Style UTF-8 Strings for Rust

Build status Crate version rustc version

This project makes it easier to establish guarantees when interfacing with nul-terminated C string APIs that require UTF-8 encoding.

Documentation

Become a Patron! Buy me a coffee

What is UTF-8?

UTF-8 is the character encoding chosen by much of the programming community since 2008, including Rust with its str primitive.

The usage of the main encodings on the web as recorded by Google

UTF-8 is capable of representing all 1,112,064 code points of the Unicode standard. Code points are variable-width, ranging from 8 to 32 bits wide.

Where does UTF-8 appear in C?

UTF-8 in SDL

The Simple DirectMedia Layer (SDL) library exposes certain APIs that only interface with UTF-8 encoded C strings. Here's a potential wrapper one could create around SDL:

impl Window {
    /* ... */

    fn title(&self) -> &CUtf8 {
        unsafe {
            let title = SDL_GetWindowTitle(self.inner);
            CUtf8::from_ptr(title).unwrap()
        }
    }

    fn set_title(&mut self, title: &CUtf8) {
        unsafe {
            SDL_SetWindowTitle(self.inner, title.as_ptr());
        }
    }

    /* ... */
}

Creating a &CUtf8 instance to interface with the above code can be done easily via the c_utf8! macro:

window.set_title(c_utf8!("MyAwesomeApp"));

Installation

This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

[dependencies]
c_utf8 = "0.1.0"

and this to your crate root (lib.rs or main.rs):

#[macro_use]
extern crate c_utf8;

License

This project is licensed under either of

at your option.

c-utf8-rs's People

Contributors

nvzqz avatar kupiakos 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.