• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
timj88

mixing unicode and non-unicode

2 posts in this topic

Hi everyone,

I'm trying to figure out if I can use both strings in the same project. I just need to use common function with different strings, like the following:

 

char* a = "raw string"

wchar*  = L"unicode string"

 

and use the same function like

 

strinLength(a), stringLength(b)

 

I'm looking for some template functions which can handle both strings, maybe something inside the std ? I'm not sure if find, replace etc.. provided in <algorithm> can handle this problem, any idea? Because I also need to use a lot of string util functions, not just the length.

 

Thanks,

J

0

Share this post


Link to post
Share on other sites

Oh windows. wchar_t, wide strings, and so on are NOT really the only way or even the best way to "do Unicode". In general you should expect a Unicode-aware strlen type function to step through every character of the string and compute length as it goes, even with wide characters there are some code points that will take multiple characters. The obvious and least painful solution is to just avoid taking string lengths, this is particularly good because there are some characters that may have length one or two depending on how you look at them. Another option would be to convert stuff to some massive 32bit length strings to take the length and then convert back when you are done.

 

In general I use Unicode mode in windows just for more static checking but IMMEDIATELY convert stuff from wchars to UTF-8 narrow strings for use inside my code. On recent versions of visual studio you can use std::wstring_convert to do the narrowing/widening. If you need to use GCC you are going to have to use the C facets library. What follows is my implementation of widen and narrow, the commented code is the same function but with std::wstring_convert and likely less bugs. The C version very likely has overrun bugs and leaks and so on (but at least is ACTUALLY narrows/widens unlike most examples on the net)

	inline std::string narrow(const std::wstring& wstr) {
        std::mbstate_t state = std::mbstate_t();
        auto buffer = wstr.c_str();
        size_t len = 1 + std::wcsrtombs(nullptr, &buffer, 0, &state);
        std::vector<char> nstrbuf(len);
        std::wcsrtombs(&nstrbuf[0], &buffer, nstrbuf.size(), &state);
        return std::string(nstrbuf.data());
        //this stuff does not work in GCC, FML
        //std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
        //return converter.to_bytes(wstr);
	}
	inline std::wstring widen(const std::string& nstr) {
        std::mbstate_t state = std::mbstate_t();
        auto buffer = nstr.c_str();
        size_t len = 1 + std::mbsrtowcs(nullptr, &buffer, 0, &state);
        std::vector<wchar_t> wstrbuf(len);
        std::mbsrtowcs(&wstrbuf[0], &buffer, wstrbuf.size(), &state);
        return std::wstring(wstrbuf.data());
        //again does not work on GCC because reasons
        //std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
        //return converter.from_bytes(nstr);
	}

see http://utf8everywhere.org/ for how to handle text and an explanation of how weird windows is

 

Edit: I think the best bet for actually getting character lengths is to go ahead and widen your strings and then take the length of the wide string. Yes this is a lot of slow conversions but strings are slow /anyways/

Edited by Demos Sema
0

Share this post


Link to post
Share on other sites

wchar is just a wide (generally 2byte) character. It hasn't really got a lot to do with Unicode and was probably added to C++ before Unicode existed.

 

Unicode is just an encoding (or a family or encodings: utf-8, utf-16 and utf-32), so it's an algorithm that requires parsing and interpreting bytes.

UTF-8, for example, can be done with regular chars. Some unicode characters require up to 4 bytes, most are 1 or 2.

 

Obviously ASCII allowed us to use a fixed-width system, 1 character == 1 char == 1 byte. Not so with Unicode although that is pretty much what you get with UTF-32. Unicode is a variable-width encoding, different characters require different numbers of bytes.

 

char is fine for UTF-8. 

But of course, with Unicode, 1 char (or wchar) does not equal 1 character (code point) necessarily.

 

As for string lengths, as Demos Sema said, you have to run the decoding algorithm which means parsing the bytes, so really a sequential scan through the string. Similarly, you cannot just random-access a character at a specific index. Of course, if you can make some assumptions about the text (e.g. I'm using UTF-8 but I know there won't be any multi-byte code points - Which is practically equivalent to saying you know your string is ASCII-only) then you can just count number of char elements and you can randomly index into the string.

 

There are no functions in the C++ Standard Library that are Unicode aware. Any strlen type functions, for example, will just be counting the number of chars in the array, which is going to yield too large a number if that string is a Unicode string.

 

The width of wchar_t is implementation defined, so it isn't really useful for unicode unless you know something about the implementation. Generally it'll be 2 bytes, which is probably good-enough and so can be used for UTF-16.

 

C++11 introduced char16_t and char32_t. These clearly have a well-defined fixed-size. So if you're using UTF-16 and C++11 you would be better off using char16_t instead of wchar_t.

 

Overall I would leave all of that alone; if you're serious about using Unicode in C++ then instead take a look at the defacto ICU library. Then there's also boost::locale.

Edited by dmatter
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0