Skip to content

Commit

Permalink
tf: added $char()
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Nov 6, 2015
1 parent b896fd9 commit ae943c3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions osx/Tests/TitleFormatting.m
Expand Up @@ -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
22 changes: 22 additions & 0 deletions tf.c
Expand Up @@ -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
Expand Down Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion utf8.c
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion utf8.h
Expand Up @@ -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);
Expand Down

0 comments on commit ae943c3

Please sign in to comment.