1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

utf8に一定文字数ごとに行頭に句読点こないように改行コード入れる。

Last updated at Posted at 2015-01-01

utf8を一定文字数ごとに改行する。
その際、行頭に「、」や「。」がこないように改行をいれる。

バイト数チェックはjanssonライブラリのutf.hを使うべし。
https://github.com/akheron/jansson/blob/master/src/utf.h

aho.cpp
# include "utf.h"
//descriptionを17文字ずつ改行
//行数を返す
int TextHelper::descriptionR(std::string *description) {
    int lines = 1;
    int cnt = 0;
    const char *c = description->c_str();
    std::stringstream stream;
    int32_t codePoint = 0;
    char buf[4];
    int size;
    const char *next = utf8_iterate(c, &codePoint);
    bool pass = false;
    while (next[0] != '\0') {
        ASSERT(!utf8_encode(codePoint, buf, &size), "not false");
        c+= size;
        for(int i = 0; i<size; i++){
            stream << buf[i];
        }
        if (cnt++ > 16) {
            next = utf8_iterate(c, &codePoint);
            ASSERT(!utf8_encode(codePoint, buf, &size), "not false");
            if(codePoint == 0x3001 || codePoint == 0x3002){
                c+= size;
                for(int i = 0; i<size; i++){
                    stream << buf[i];
                }
            }
            cnt = 0;
            stream << '\n';
            lines++;
            if(next[0]== '\0')goto __skip;
        }
        next = utf8_iterate(c, &codePoint);
    }
    ASSERT(!utf8_encode(codePoint, buf, &size), "not false2");
    for(int i = 0; i<size; i++){
        stream << buf[i];
    }
__skip:
    *description = stream.str();
    return lines;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?