From ae943c3b84bcd24e5addffba91355b925850eee3 Mon Sep 17 00:00:00 2001 From: Alexey Yakovenko Date: Fri, 6 Nov 2015 13:46:17 +0100 Subject: [PATCH] tf: added $char() --- osx/Tests/TitleFormatting.m | 6 ++++++ tf.c | 22 ++++++++++++++++++++++ utf8.c | 2 +- utf8.h | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/osx/Tests/TitleFormatting.m b/osx/Tests/TitleFormatting.m index 342e7150b4..41b24bdffc 100644 --- a/osx/Tests/TitleFormatting.m +++ b/osx/Tests/TitleFormatting.m @@ -783,4 +783,10 @@ - (void)test_Caps2TestUnicodeRandomizedString_ReturnsCapitalizeEachWordString { XCTAssert(!strcmp (buffer, "ⱭBCD ⱿHİJ"), @"The actual output is: %s", buffer); } +- (void)test_Char1055And88And38899_ReturnsCorrespondingUTF8Chars { + char *bc = tf_compile("$char(1055)$char(88)$char(38899)"); + tf_eval (&ctx, bc, buffer, 1000); + XCTAssert(!strcmp (buffer, "ПX音"), @"The actual output is: %s", buffer); +} + @end diff --git a/tf.c b/tf.c index ca03fc2a94..5715ad95d7 100644 --- a/tf.c +++ b/tf.c @@ -371,6 +371,27 @@ tf_func_caps2 (ddb_tf_context_t *ctx, int argc, char *arglens, char *args, char return tf_caps_impl (ctx, argc, arglens, args, out, outlen, fail_on_undef, 0); } +int +tf_func_char (ddb_tf_context_t *ctx, int argc, char *arglens, char *args, char *out, int outlen, int fail_on_undef) { + if (argc != 1) { + return -1; + } + + int bool_out = 0; + + int len; + TF_EVAL_CHECK(len, ctx, args, arglens[0], out, outlen, fail_on_undef); + + int n = atoi (out); + *out = 0; + + if (outlen < 5) { + return -1; + } + len = u8_wc_toutf8 (out, n); + out[len] = 0; + return len; +} // $left(text,n) returns the first n characters of text int @@ -960,6 +981,7 @@ tf_func_def tf_funcs[TF_MAX_FUNCS] = { { "ascii", tf_func_ascii }, { "caps", tf_func_caps }, { "caps2", tf_func_caps2 }, + { "char", tf_func_char }, { "cut", tf_func_left }, { "left", tf_func_left }, // alias of 'cut' { "strcmp", tf_func_strcmp }, diff --git a/utf8.c b/utf8.c index 3aec97bf6d..06a555785b 100644 --- a/utf8.c +++ b/utf8.c @@ -158,7 +158,7 @@ int u8_toutf8(char *dest, int32_t sz, uint32_t *src, int32_t srcsz) return i; } -int u8_wc_toutf8(char *dest, unsigned long ch) +int u8_wc_toutf8(char *dest, uint32_t ch) { if (ch < 0x80) { dest[0] = (char)ch; diff --git a/utf8.h b/utf8.h index 4d19a73b55..a39b1fd754 100644 --- a/utf8.h +++ b/utf8.h @@ -47,7 +47,7 @@ int u8_toucs(uint32_t *dest, int32_t sz, const char *src, int32_t srcsz); int u8_toutf8(char *dest, int32_t sz, uint32_t *src, int32_t srcsz); /* single character to UTF-8 */ -int u8_wc_toutf8(char *dest, unsigned long ch); +int u8_wc_toutf8(char *dest, uint32_t ch); /* character number to byte offset */ int u8_offset(char *str, int32_t charnum);