File: encode_wide_string_as_utf8.c

package info (click to toggle)
putty 0.83-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,216 kB
  • sloc: ansic: 148,476; python: 8,466; perl: 1,830; makefile: 128; sh: 117
file content (23 lines) | stat: -rw-r--r-- 571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Encode a string of wchar_t as UTF-8.
 */

#include "putty.h"
#include "misc.h"

char *encode_wide_string_as_utf8(const wchar_t *ws)
{
    strbuf *sb = strbuf_new();
    while (*ws) {
        unsigned long ch = *ws++;
        if (sizeof(wchar_t) == 2 && IS_HIGH_SURROGATE(ch) &&
            IS_LOW_SURROGATE(*ws)) {
            ch = FROM_SURROGATES(ch, *ws);
            ws++;
        } else if (IS_SURROGATE(ch)) {
            ch = 0xfffd; /* illegal UTF-16 -> REPLACEMENT CHARACTER */
        }
        put_utf8_char(sb, ch);
    }
    return strbuf_to_str(sb);
}