# We need(or not) strlen in utf-8?

**URL:** <https://discourse.cataclysmdda.org/t/we-need-or-not-strlen-in-utf-8/2780>\
**Category:** The Garage\
**Created:** [August 21, 2013, 4:44pm UTC](https://discourse.cataclysmdda.org/t/we-need-or-not-strlen-in-utf-8/2780 "2013-08-21T16:44:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LazyCat-rus](https://avatars.discourse-cdn.com/v4/letter/l/258eb7/32.png) [@LazyCat-rus](https://discourse.cataclysmdda.org/u/LazyCat-rus)\
**Post date:** [August 21, 2013, 4:44pm UTC](https://discourse.cataclysmdda.org/t/we-need-or-not-strlen-in-utf-8/2780/1 "2013-08-21T16:44:57Z")

</div>

Hello.  
I need your help with issue [#2575](https://github.com/CleverRaven/Cataclysm-DDA/issues/2575).

I made path than fix this issue.  
Here is path:

[code]diff --git a/050c0d1 b/09f3336  
index 050c0d1…09f3336 100644  
— a/050c0d1  
+++ b/09f3336  
@@ -4559,15 +4559,10 @@ int game::mon\_info(WINDOW \*w)  
for (int i = 0; i \< 8; i++) {  
widths[i] = strlen(dir\_labels[i]);  
}

- const int row1spaces = width - (widths[7] + widths[0] + widths[1]);
- const int row3spaces = width - (widths[5] + widths[4] + widths[3]);  
int xcoords[8];  
const int ycoords[] = { 0, 0, 1, 2, 2, 2, 1, 0 };
- xcoords[0] = widths[7] + row1spaces / 3;
- xcoords[1] = widths[7] - (row1spaces / 3) + row1spaces + widths[0];
- xcoords[4] = widths[5] + row3spaces / 3;
- xcoords[3] = widths[5] - (row3spaces / 3) + row3spaces + widths[4];
- xcoords[2] = (xcoords[1] + xcoords[3]) / 2;

- xcoords[0] = xcoords[4] = width / 3;
- xcoords[1] = xcoords[3] = xcoords[2] = (width / 3) \* 2;  
xcoords[5] = xcoords[6] = xcoords[7] = 0;  
for (int i = 0; i \< 8; i++) {  
nc\_color c = unique\_types[i].empty() ? c\_dkgray  
@@ -4575,13 +4570,13 @@ int game::mon\_info(WINDOW \*w)  
mvwprintz(w, ycoords[i] + startrow, xcoords[i], c, dir\_labels[i]);  
}

- // The list of symbols needs a space on each end.
- const int symroom = row1spaces / 3 - 2;
- // Print the symbols of all monsters in all directions.  
for (int i = 0; i \< 8; i++) {

- 

```
   int symroom;
   point pr(xcoords[i] + strlen(dir_labels[i]) + 1, ycoords[i] + startrow);

```

- 

```
   // The list of symbols needs a space on each end.

```

- 

```
   symroom = (width / 3) - widths[i] - 2;
   const int typeshere = unique_types[i].size();
   for (int j = 0; j < typeshere && j < symroom; j++) {
       buff = unique_types[i][j];[/code]

```

It’s simple patch, witch makes all column’s direction will be divided info 3 equal parts. Every row will be aligned.  
Yes, it works with non-English languages, which uses Latin symbols. Before patch columns were unaligned in these languages.  
For not Latin languages, my patch can’t resolve issue. Game has to know length of all direction’s strings and it’s length is made with strlen(). So, gettext() return string in utf-8, but strlen() can’t work with utf-8 strings. strlen() returns number of ‘char’ type. For Latin symbols is’t ok, but for utf-8 string ‘char’ != character.  
I have googled and have found [primitive](http://stackoverflow.com/questions/5117393/utf-8-strings-length-in-linux-c) strlen() for utf-8. I have made a fast patch which replace strlen() to strlen\_utf8() and before patch worked for Russian language(for others too, but I hadn’t checked).  
At once game will have to work with unicode(utf-8 or other). That’s point of i18n. All calculation based on length of translatable strings and its will must use unicode\_len().  
I can add strlen\_utf-8() in pull request(I hadn’t created yet) which resolves issue #2575, but I don’t know in what file I should place my code. Furthermore, if gettext support will be disabled on compile time, how can it will be used strlen() instead of strlen\_utf8()?

```auto
#ifdef TRANSLATION FLAG
... definition of strlen_utf8()
#define strlen(string) strlen_utf8(string)
#endif
```

If this is acceptable, i will write needed patches.

---

<div class="post-metadata">

**Author:** ![kevin.granade](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/kevin.granade/32/1316_2.png) [@kevin.granade](https://discourse.cataclysmdda.org/u/kevin.granade)\
**Post date:** [August 21, 2013, 6:49pm UTC](https://discourse.cataclysmdda.org/t/we-need-or-not-strlen-in-utf-8/2780/2 "2013-08-21T18:49:08Z")

</div>

You are correct, and a fix for this would be appreciated, but there is already a utf-8 aware string length function, utf8\_width() defined in catacharset.cpp, so no need to import another one.

While gettext is optional to make building on windows easier, I don’t think there’s an easy way to make unicode support optional, so any utf8 handling code will not be conditional. Also we will be adding utf-8 glyphs for better looking maps and such, so we will want to handle them even in the english version.

Thanks for addressing this.
