Sha256: a5279178a84382855cbef2cdb7014cff41ef7b5ff08b16f9cf6dd3780780bf3a

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

#ifndef _IV_UNICODE_H_
#define _IV_UNICODE_H_
#include <cassert>
#include <tr1/cstdint>
#include <tr1/array>
namespace iv {
namespace core {

inline int UCS4ToUTF8(uint32_t uc, uint8_t* buf) {
  assert(uc <= 0x10FFFF);
  if (uc < 0x80) {
    // ASCII range
    *buf = static_cast<uint8_t>(uc);
    return 1;
  } else {
    uint32_t a = uc >> 11;
    int len = 2;
    while (a) {
      a >>= 5;
      ++len;
    }
    int i = len;
    while (--i) {
      buf[i] = static_cast<uint8_t>((uc & 0x3F) | 0x80);
      uc >>= 6;
    }
    *buf = static_cast<uint8_t>(0x100 - (1 << (8 - len)) + uc);
    return len;
  }
}

inline uint32_t UTF8ToUCS4(const uint8_t* buf, uint32_t size) {
  static const std::tr1::array<uint32_t, 3> kMinTable = { {
    0x00000080, 0x00000800, 0x00010000
  } };
  assert(size >= 1 && size <= 4);
  if (size == 1) {
    assert(!(*buf & 0x80));
    return *buf;
  } else {
    assert((*buf & (0x100 - (1 << (7 - size)))) == (0x100 - (1 << (8 - size))));
    uint32_t uc = (*buf++) & ((1 << (7 - size)) - 1);
    const uint32_t min = kMinTable[size-2];
    while (--size) {
      assert((*buf & 0xC0) == 0x80);
      uc = uc << 6 | (*buf++ & 0x3F);
    }
    if (uc < min) {
      uc = UINT32_MAX;
    }
    return uc;
  }
}

} }  // namespace iv::core
#endif  // _IV_UNICODE_H_

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iv-phonic-0.1.8 ext/include/iv/unicode.h