Exemplo n.º 1
1
ssize_t
utf8_to_utf32(uint32_t      *out,
              const uint8_t *in,
              size_t        len)
{
  ssize_t  rc = 0;
  ssize_t  units;
  uint32_t code;

  do
  {
    units = decode_utf8(&code, in);
    if(units == -1)
      return -1;

    if(code > 0)
    {
      in += units;

      if(out != NULL)
      {
        if(rc < len)
          *out++ = code;
      }

      if(SSIZE_MAX - 1 >= rc)
        ++rc;
      else
        return -1;
    }
  } while(code > 0);

  return rc;
}
Exemplo n.º 2
1
void TextEngine::renderText(float x, float y, float scaleX, float scaleY, bool baseline, const char* text)
{		
	ssize_t  units;
	uint32_t code;

	// Configure buffers
	C3D_BufInfo* bufInfo = C3D_GetBufInfo();
	BufInfo_Init(bufInfo);
	BufInfo_Add(bufInfo, textVtxArray, sizeof(textVertex_s), 2, 0x10);

	const uint8_t* p = (const uint8_t*)text;
	float firstX = x;
	u32 flags = GLYPH_POS_CALC_VTXCOORD | (baseline ? GLYPH_POS_AT_BASELINE : 0);
	int lastSheet = -1;
	do
	{
		if (!*p) break;
		units = decode_utf8(&code, p);
		if (units == -1)
			break;
		p += units;
		if (code == '\n')
		{
			x = firstX;
			y += scaleY*fontGetInfo()->lineFeed;
		}
		else if (code > 0)
		{
			int glyphIdx = fontGlyphIndexFromCodePoint(code);
			fontGlyphPos_s data;
			fontCalcGlyphPos(&data, glyphIdx, flags, scaleX, scaleY);

			// Bind the correct texture sheet
			if (data.sheetIndex != lastSheet)
			{
				lastSheet = data.sheetIndex;
				C3D_TexBind(0, &glyphSheets[lastSheet]);
			}

			int arrayIndex = textVtxArrayPos;
			if ((arrayIndex+4) >= TEXT_VTX_ARRAY_COUNT)
				break; // We can't render more characters

			// Add the vertices to the array
			addTextVertex(x+data.vtxcoord.left,  y+data.vtxcoord.bottom, data.texcoord.left,  data.texcoord.bottom);
			addTextVertex(x+data.vtxcoord.right, y+data.vtxcoord.bottom, data.texcoord.right, data.texcoord.bottom);
			addTextVertex(x+data.vtxcoord.left,  y+data.vtxcoord.top,    data.texcoord.left,  data.texcoord.top);
			addTextVertex(x+data.vtxcoord.right, y+data.vtxcoord.top,    data.texcoord.right, data.texcoord.top);

			// Draw the glyph
			C3D_DrawArrays(GPU_TRIANGLE_STRIP, arrayIndex, 4);

			x += data.xAdvance;

		}
	} while (code > 0);
}
Exemplo n.º 3
0
bool LoadMETA(Puzzle * puz, const std::string & data)
{
    std::istringstream stream(data);
    istream_wrapper f(stream);

    while (! f.CheckEof())
        puz->SetMeta(decode_utf8(f.ReadString()), decode_utf8(f.ReadString()));
    return true;
}
Exemplo n.º 4
0
string_t Parser::GetText(node n)
{
    Visit(n);
    std::ostringstream stream;
    TextToStream(n, stream);
    return decode_utf8(stream.str());
}
Exemplo n.º 5
0
String jsonnet_string_escape(const String &str, bool single)
{
    StringStream ss;
    for (std::size_t i=0 ; i<str.length() ; ++i) {
        char32_t c = str[i];
        switch (c) {
            case U'\"': ss << (single ? U"\"" : U"\\\""); break;
            case U'\'': ss << (single ? U"\\\'" : U"\'"); break;
            case U'\\': ss << U"\\\\"; break;
            case U'\b': ss << U"\\b"; break;
            case U'\f': ss << U"\\f"; break;
            case U'\n': ss << U"\\n"; break;
            case U'\r': ss << U"\\r"; break;
            case U'\t': ss << U"\\t"; break;
            case U'\0': ss << U"\\u0000"; break;
            default: {
                if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) {
                    //Unprintable, use \u
                    std::stringstream ss8;
                    ss8 << "\\u" << std::hex << std::setfill('0') << std::setw(4)
                       << (unsigned long)(c);
                    ss << decode_utf8(ss8.str());
                } else {
                    // Printable, write verbatim
                    ss << c;
                }
            }
        }
    }
    return ss.str();
}
Exemplo n.º 6
0
    void desugarFile(AST *&ast)
    {
        desugar(ast, 0);

        // Now, implement the std library by wrapping in a local construct.
        Tokens tokens = jsonnet_lex("std.jsonnet", STD_CODE);
        AST *std_ast = jsonnet_parse(alloc, tokens);
        desugar(std_ast, 0);
        auto *std_obj = dynamic_cast<DesugaredObject*>(std_ast);
        if (std_obj == nullptr) {
            std::cerr << "INTERNAL ERROR: std.jsonnet not an object." << std::endl;
            std::abort();
        }

        // Bind 'std' builtins that are implemented natively.
        DesugaredObject::Fields &fields = std_obj->fields;
        for (unsigned long c=0 ; c <= max_builtin ; ++c) {
            const auto &decl = jsonnet_builtin_decl(c);
            Identifiers params;
            for (const auto &p : decl.params)
                params.push_back(alloc->makeIdentifier(p));
            fields.emplace_back(
                ObjectField::HIDDEN,
                str(decl.name),
                alloc->make<BuiltinFunction>(E, c, params));
        }
        fields.emplace_back(
            ObjectField::HIDDEN,
            str(U"thisFile"),
            str(decode_utf8(ast->location.file)));

        ast = alloc->make<Local>(ast->location, EF, singleBind(id(U"std"), std_obj), ast);
    }
Exemplo n.º 7
0
string_t Parser::GetInnerXML(node n)
{
    Visit(n);
    std::ostringstream stream;
    for (node child = n.first_child(); child; child = child.next_sibling())
        child.print(stream, "", pugi::format_raw);
    return decode_utf8(stream.str());
}
Exemplo n.º 8
0
int MainCaitra::letter_string_edit_distance( Word wordId1, Word wordId2 ) {
    wstring word1;
    decode_utf8(surface[ wordId1 ], word1);
    wstring word2;
    decode_utf8(surface[ wordId2 ], word2);
    int **cost = (int**) calloc( sizeof( int* ), word1.size() );
    for( int i=0; i<word1.size(); i++ ) {
        cost[i] = (int*) calloc( sizeof( int ), word2.size() );
        for( int j=0; j<word2.size(); j++ ) {
            if (i==0 && j==0) {
                cost[i][j] = 0;
            }
            else {
                cost[i][j] = 999;
                if (j>0 && cost[i][j-1]+1 < cost[i][j]) {
                    cost[i][j] = cost[i][j-1]+1;
                }
                if (i>0 && cost[i-1][j]+1 < cost[i][j]) {
                    cost[i][j] = cost[i-1][j]+1;
                }
                if (i>0 && j>0) {
                    if (word1[i] != word2[j]) {
                        if (cost[i-1][j-1]+1 < cost[i][j]) {
                            cost[i][j] = cost[i-1][j-1]+1;
                        }
                    }
                    else {
                        if (cost[i-1][j-1] < cost[i][j]) {
                            cost[i][j] = cost[i-1][j-1];
                        }
                    }
                }

            }
        }

    }
    int distance = cost[word1.size()-1][word2.size()-1];
    for( int i=0; i<word1.size(); i++ ) {
        free( cost[i] );
    }
    free( cost );
    return distance;
}
UTF8Iterator::UTF8Iterator(const std::string& text_) :
  text(text_),
  pos(0),
  chr()
{
  try {
    chr = decode_utf8(text, pos);
  } catch (std::exception) {
    log_debug << "Malformed utf-8 sequence beginning with " << *((uint32_t*)(text.c_str() + pos)) << " found " << std::endl;
    chr = 0;
  }
}
  UTF8Iterator& 
UTF8Iterator::operator++() {
    try {
      chr = decode_utf8(text, pos);
    } catch (std::exception) {
      log_debug << "Malformed utf-8 sequence beginning with " << *((uint32_t*)(text.c_str() + pos)) << " found " << std::endl;
      chr = 0;
      ++pos;
    }

    return *this;
  }
Exemplo n.º 11
0
static int
get_utf8(const unsigned char *p, size_t len, ICONV_CHAR *out)
{
	uint32_t uc, state = 0;
	size_t l = 1;

	do {
		switch (decode_utf8(&state, &uc, *p++)) {
		case 0:
			*out = uc;
			return l;
		case UTF8_REJECT:
			return -EILSEQ;
		}
	} while (l++ < len);
	return -EINVAL;
}
Exemplo n.º 12
0
    void FontRenderer::DrawChar(FontFaceType type, Canvas canvas, Position pos, Colour colour, const char c)
    {
        FT_Face face = m_ftFaces[type];
        u32 utf8Char = 0;
        ssize_t unitCount = decode_utf8(&utf8Char, (const uint8_t*)&c);

        if (unitCount <= 0)
            return;

        FT_UInt glyphIndex = FT_Get_Char_Index(face, utf8Char);

        if (R_FAILED(FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT)))
            return;

        if (R_FAILED(FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)))
            return;
            
        FT_GlyphSlot slot = face->glyph;
        u32 x = pos.x + slot->bitmap_left;
        u32 y = pos.y - slot->bitmap_top;
        FT_Bitmap* bitmap = &slot->bitmap;
        u8* imageptr = bitmap->buffer;

        if (bitmap->pixel_mode != FT_PIXEL_MODE_GRAY) 
            return;

        for (u32 tmpy = 0; tmpy < bitmap->rows; tmpy++)
        {
            for (u32 tmpx = 0; tmpx < bitmap->width; tmpx++)
            {
                colour.a = imageptr[tmpx];

                // Skip blanks
                if (!colour.a)
                    continue;

                canvas.DrawPixelBlend(x + tmpx, y + tmpy, colour);
            }

            imageptr += bitmap->pitch;
        }
    }
Exemplo n.º 13
0
// From CamelCase to snake_case
string_t snake_case(const char * name)
{
    // Read the char string into a std::string, replacing
    // capital letters with '_' plus the lower case letter
    // utf-8 issues shouldn't be a problem since isupper is only
    // true for < 128
    std::string str;
    for (int i = 0; name[i]; ++i)
    {
        if (::isupper(name[i]))
        {
            if (i > 0)
                str.push_back('_');
            str.push_back(::tolower(name[i]));
        }
        else
            str.push_back(name[i]);
    }
    return decode_utf8(str);
}
static void string_reverse_implementation(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    const unsigned char* input = 0;
    int input_type;
    sqlite_uint64 input_length;
    char* result;
    int* decoded;

    if (argc < 1) {
        sqlite3_result_error(ctx, "not enough parameters", -1);
        return;
    }

    if (argc > 1) {
        sqlite3_result_error(ctx, "too many parameters", -1);
        return;
    }

    input_type = sqlite3_value_type(argv[0]);

    if (input_type != SQLITE_NULL) {
        input = sqlite3_value_text(argv[0]);
    }

    if (input == 0) {
        sqlite3_result_null(ctx);
        return;
    }

    input_length = strlen_utf8(input);
    result = sqlite3_malloc(4 * input_length + 1);
    decoded = sqlite3_malloc(sizeof(int) * input_length);

    decode_utf8(input, decoded);
    reverse_string(decoded, result, input_length);

    sqlite3_free(decoded);

    sqlite3_result_text(ctx, result, -1, result_string_destructor);
}
Exemplo n.º 15
0
/*! Initialize SDMC device */
Result sdmcInit(void)
{
  ssize_t  units;
  uint32_t code;
  char     *p;
  FS_Path sdmcPath = { PATH_EMPTY, 1, (u8*)"" };
  Result   rc = 0;

  if(sdmcInitialised)
    return rc;


  rc = FSUSER_OpenArchive(&sdmcArchive, ARCHIVE_SDMC, sdmcPath);
  if(R_SUCCEEDED(rc))
  {
    fsExemptFromSession(sdmcArchive);

    int dev = AddDevice(&sdmc_devoptab);

    if(dev != -1)
    {
      setDefaultDevice(dev);
      if(__system_argc != 0 && __system_argv[0] != NULL)
      {
        if(FindDevice(__system_argv[0]) == dev)
        {
          strncpy(__fixedpath,__system_argv[0],PATH_MAX);
          if(__fixedpath[PATH_MAX] != 0)
          {
            __fixedpath[PATH_MAX] = 0;
          }
          else
          {
            char *last_slash = NULL;
            p = __fixedpath;
            do
            {
              units = decode_utf8(&code, (const uint8_t*)p);
              if(units < 0)
              {
                last_slash = NULL;
                break;
              }

              if(code == '/')
                last_slash = p;

              p += units;
            } while(code != 0);

            if(last_slash != NULL)
            {
              last_slash[0] = 0;
              chdir(__fixedpath);
            }
          }
        }
      }
    }
  }

  sdmcInitialised = true;

  return rc;
}
Exemplo n.º 16
0
static const char*
sdmc_fixpath(struct _reent *r,
             const char    *path)
{
  ssize_t       units;
  uint32_t      code;
  const uint8_t *p = (const uint8_t*)path;

  // Move the path pointer to the start of the actual path
  do
  {
    units = decode_utf8(&code, p);
    if(units < 0)
    {
      r->_errno = EILSEQ;
      return NULL;
    }

    p += units;
  } while(code != ':' && code != 0);

  // We found a colon; p points to the actual path
  if(code == ':')
    path = (const char*)p;

  // Make sure there are no more colons and that the
  // remainder of the filename is valid UTF-8
  p = (const uint8_t*)path;
  do
  {
    units = decode_utf8(&code, p);
    if(units < 0)
    {
      r->_errno = EILSEQ;
      return NULL;
    }

    if(code == ':')
    {
      r->_errno = EINVAL;
      return NULL;
    }

    p += units;
  } while(code != 0);

  if(path[0] == '/')
    strncpy(__fixedpath, path, PATH_MAX);
  else
  {
    strncpy(__fixedpath, __cwd, PATH_MAX);
    __fixedpath[PATH_MAX] = '\0';
    strncat(__fixedpath, path, PATH_MAX);
  }

  if(__fixedpath[PATH_MAX] != 0)
  {
    __fixedpath[PATH_MAX] = 0;
    r->_errno = ENAMETOOLONG;
    return NULL;
  }

  return __fixedpath;
}
Exemplo n.º 17
0
void GfxTextBuffer::addFormattedString (const std::string &text,
                                        const Vector3 &top_colour, float top_alpha,
                                        const Vector3 &bot_colour, float bot_alpha)
{
    Vector3 ansi_colour[] = {
        Vector3(0,0,0), Vector3(.8,0,0), Vector3(0,.8,0), Vector3(.8,.8,0),
        Vector3(0,0,.93), Vector3(.8,0,.8), Vector3(0,.8,.8), Vector3(.75,.75,.75),
    };
    Vector3 ansi_bold_colour[] = {
        Vector3(0.5,0.5,0.5), Vector3(1,0,0), Vector3(0,1,0), Vector3(1,1,0),
        Vector3(.36,.36,1), Vector3(1,0,1), Vector3(0,1,1), Vector3(1,1,1),
    };

    ColouredChar c(0, top_colour, top_alpha, bot_colour, bot_alpha);

    unsigned long original_size = colouredText.size();

    bool bold = false;
    unsigned default_colour = 7; // FIXME: this assumes a 'white on black' console
    unsigned last_colour = default_colour;
    for (size_t i=0 ; i<text.length() ; ++i) {
        GfxFont::codepoint_t cp = decode_utf8(text, i);
        if (cp == UNICODE_ESCAPE_CODEPOINT) {
            if (++i >= text.length()) break;
            cp = decode_utf8(text, i);
            if (cp == '[') {
                // continue until 'm'
                unsigned code = 0;
                do {
                    if (++i >= text.length()) break;
                    cp = decode_utf8(text, i);
                    if (cp == 'm' || cp == ';') {
                        // act on code
                        if (code == 0) {
                            bold = false;
                            last_colour = default_colour;
                            c.topColour = top_colour;
                            c.bottomColour = bot_colour;
                        } else if (code == 1) {
                            bold = true;
                            Vector3 col = bold ? ansi_bold_colour[last_colour] : ansi_colour[last_colour];
                            c.topColour = col;
                            c.bottomColour = col;
                        } else if (code == 22) {
                            bold = false;
                            Vector3 col = bold ? ansi_bold_colour[last_colour] : ansi_colour[last_colour];
                            c.topColour = col;
                            c.bottomColour = col;
                        } else if (code >= 30 && code <= 37) {
                            last_colour = code - 30;
                            Vector3 col = bold ? ansi_bold_colour[last_colour] : ansi_colour[last_colour];
                            c.topColour = col;
                            c.bottomColour = col;
                        }
                        code = 0;
                    }
                    if (cp == 'm') break;
                    if (cp >= '0' && cp <= '9') {
                        unsigned digit = cp - '0';
                        code = 10*code + digit;
                    }
                } while (true);
                if (cp == 'm') continue;
            }
        }
        
        // This char is not part of an ansi terminal colour code.
        c.cp = cp;
        colouredText.push_back(c);
    }

    recalculatePositions(original_size);
}
Exemplo n.º 18
0
void decode_cell(pTHX_ unsigned char *input, STRLEN len, STRLEN *pos, struct cc_type *type, SV *output)
{
    unsigned char *bytes;
    STRLEN bytes_len;

    if (unpack_bytes(aTHX_ input, len, pos, &bytes, &bytes_len) != 0) {
        sv_setsv(output, &PL_sv_undef);
        return;
    }

    switch (type->type_id) {
        case CC_TYPE_ASCII:
        case CC_TYPE_CUSTOM:
        case CC_TYPE_BLOB:
            decode_blob(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_BOOLEAN:
            decode_boolean(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_VARCHAR:
        case CC_TYPE_TEXT:
            decode_utf8(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_INET:
            decode_inet(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_SET:
        case CC_TYPE_LIST:
            decode_list(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_UUID:
        case CC_TYPE_TIMEUUID:
            decode_uuid(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_FLOAT:
            decode_float(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_DOUBLE:
            decode_double(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_DECIMAL:
            decode_decimal(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_VARINT:
        case CC_TYPE_BIGINT:
        case CC_TYPE_COUNTER:
        case CC_TYPE_TIMESTAMP:
        case CC_TYPE_SMALLINT:
        case CC_TYPE_TINYINT:
        case CC_TYPE_INT:
            decode_varint(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_DATE:
            decode_date(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_TIME:
            decode_time(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_MAP:
            decode_map(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_UDT:
            decode_udt(aTHX_ bytes, bytes_len, type, output);
            break;

        case CC_TYPE_TUPLE:
            decode_tuple(aTHX_ bytes, bytes_len, type, output);
            break;

        default:
            sv_setsv(output, &PL_sv_undef);
            warn("Decoder doesn't yet understand type %d, returning undef instead", type->type_id);
            break;
    }
}
Exemplo n.º 19
0
inline string_t
Parser::GetAttribute(const node n, const char * name)
{
    return decode_utf8(n.attribute(name).value());
}
Exemplo n.º 20
0
uint32_t
UTF8::decode_utf8(const std::string& text)
{
  size_t p = 0;
  return decode_utf8(text, p);
}
Exemplo n.º 21
0
OutputIterator utf8_wchar(InputIterator first, InputIterator last, OutputIterator dest)
{
	for(; first!=last; ++dest)
		*dest = decode_utf8(first, last);
	return dest;
}
Exemplo n.º 22
0
/*
 * v_key_name --
 *	Return the string that will display the key.  This routine
 *	is the backup for the KEY_NAME() macro.
 *
 * PUBLIC: char *v_key_name __P((SCR *, ARG_CHAR_T));
 */
char *
v_key_name(
	SCR *sp,
	ARG_CHAR_T ach)
{
	static const char hexdigit[] = "0123456789abcdef";
	static const char octdigit[] = "01234567";
	int ch;
	size_t len;
	char *chp;

	/*
	 * Cache the last checked character.  It won't be a problem
	 * since nvi will rescan the mapping when settings changed.
	 */
	if (ach && sp->lastc == ach)
		return (sp->cname);
	sp->lastc = ach;

#ifdef USE_WIDECHAR
	len = wctomb(sp->cname, ach);
	if (len > MB_CUR_MAX)
#endif
		sp->cname[(len = 1)-1] = (u_char)ach;

	ch = (u_char)sp->cname[0];
	sp->cname[len] = '\0';

	/* See if the character was explicitly declared printable or not. */
	if ((chp = O_STR(sp, O_PRINT)) != NULL)
		if (strstr(chp, sp->cname) != NULL)
			goto done;
	if ((chp = O_STR(sp, O_NOPRINT)) != NULL)
		if (strstr(chp, sp->cname) != NULL)
			goto nopr;

	/*
	 * Historical (ARPA standard) mappings.  Printable characters are left
	 * alone.  Control characters less than 0x20 are represented as '^'
	 * followed by the character offset from the '@' character in the ASCII
	 * character set.  Del (0x7f) is represented as '^' followed by '?'.
	 *
	 * XXX
	 * The following code depends on the current locale being identical to
	 * the ASCII map from 0x40 to 0x5f (since 0x1f + 0x40 == 0x5f).  I'm
	 * told that this is a reasonable assumption...
	 *
	 * XXX
	 * The code prints non-printable wide characters in 4 or 5 digits
	 * Unicode escape sequences, so only supports plane 0 to 15.
	 */
	if (ISPRINT(ach))
		goto done;
nopr:	if (iscntrl(ch) && (ch < 0x20 || ch == 0x7f)) {
		sp->cname[0] = '^';
		sp->cname[1] = ch == 0x7f ? '?' : '@' + ch;
		len = 2;
		goto done;
	}
#ifdef USE_WIDECHAR
	if (INTISWIDE(ach)) {
		int uc = -1;

		if (!strcmp(codeset(), "UTF-8"))
			uc = decode_utf8(sp->cname);
#ifdef USE_ICONV
		else {
			char buf[sizeof(sp->cname)] = "";
			size_t left = sizeof(sp->cname);
			char *in = sp->cname;
			char *out = buf;
			iconv(sp->conv.id[IC_IE_TO_UTF16],
			    (iconv_src_t)&in, &len, &out, &left);
			iconv(sp->conv.id[IC_IE_TO_UTF16],
			    NULL, NULL, NULL, NULL);
			uc = decode_utf16(buf, 1);
		}
#endif
		if (uc >= 0) {
			len = snprintf(sp->cname, sizeof(sp->cname),
			    uc < 0x10000 ? "\\u%04x" : "\\U%05X", uc);
			goto done;
		}
	}
#endif
	if (O_ISSET(sp, O_OCTAL)) {
		sp->cname[0] = '\\';
		sp->cname[1] = octdigit[(ch & 0300) >> 6];
		sp->cname[2] = octdigit[(ch &  070) >> 3];
		sp->cname[3] = octdigit[ ch &   07      ];
	} else {