Giter Club home page Giter Club logo

unifill's Introduction

Unifill

Shim your code to support Unicode across all platforms.

Usage

The declaration using unifill.Unifill; introduces the methods whose name starts with u into String instances. Replace all methods of Strings in your code with Unifill's methods, and your code will be able to deal with Unicode strings across all platforms.

using unifill.Unifill;
import unifill.CodePoint;

class Main {
  public static function main() : Void {
    trace("日本語".uLength()); // ==> 3
    trace("русский".uCharAt(5)); // ==> и
    trace("🍺".uCodePointAt(0).toInt()); // ==> 127866
    trace(CodePoint.fromInt(0x1F37B)); // ==> 🍻
    for (c in "♠♡♢♣".uIterator()) {
      trace(c);
      trace(c + 4);
    }
  }
}

Iteration

You might write for loops like this:

function f(s : String) : Void {
  for (i in s.uLength()) {
    trace(s.uCharAt(i));
  }
}

But this way may be inefficient because f(s) has order of the square of the length of s.

Instead, you can use uIterator to make the function linear time:

function f(s : String) : Void {
  for (c in s.uIterator()) {
    trace(c.toString());
  }
}

uIterator iterates over each code point in the string.

InternalEncoding

For advanced usage, you can use InternalEncoding, which provides methods treating variable-length encoding without considering which encoding form is practically used.

These methods index by code units. That is, the value of InternalEncoding.charAt("эюя", 2) varies depending the tar environment: the UTF-8 environments (Haxe macro, C++, Neko, PHP and Lua) give "ю", while the UTF-16 environments (Flash, C#, Java and JavaScript) and the UTF-32 environment (Python) give "я".

InternalEncoding.codePointWidthAt returns the number of code units the code point is consist of, so any platform gives "ю" for the following expression:

InternalEncoding.charAt("эюя", InternalEncoding.codePointWidthAt("эюя", 0))

unifill's People

Contributors

mandel59 avatar

Watchers

James Cloos avatar David Klein 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.