Skip to content

Commit

Permalink
tf: fix implementation of $longer (fixes #3087)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed May 3, 2024
1 parent 6b69ff9 commit 614a94e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 6 additions & 6 deletions Tests/TitleFormattingTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2084,18 +2084,18 @@ TEST_F(TitleFormattingTests, test_LongestMid_ReturnsMid) {
EXPECT_TRUE(!strcmp (buffer, "333"));
}

TEST_F(TitleFormattingTests, test_LongerFirst_ReturnsFirst) {
char *bc = tf_compile("$longer(22,1)");
TEST_F(TitleFormattingTests, test_LongerFirst_ReturnsTrue) {
char *bc = tf_compile("$if($longer(22,1),true,false)");
tf_eval (&ctx, bc, buffer, 1000);
tf_free (bc);
EXPECT_TRUE(!strcmp (buffer, "22"));
EXPECT_TRUE(!strcmp (buffer, "true"));
}

TEST_F(TitleFormattingTests, test_LongerSecond_ReturnsSecond) {
char *bc = tf_compile("$longer(1,22)");
TEST_F(TitleFormattingTests, test_LongerSecond_ReturnsFalse) {
char *bc = tf_compile("$if($longer(1,22),true,false)");
tf_eval (&ctx, bc, buffer, 1000);
tf_free (bc);
EXPECT_TRUE(!strcmp (buffer, "22"));
EXPECT_TRUE(!strcmp (buffer, "false"));
}

TEST_F(TitleFormattingTests, test_PadcutStrLonger_ReturnsHeadOfStr) {
Expand Down
14 changes: 13 additions & 1 deletion src/tf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,19 @@ tf_func_longer(ddb_tf_context_t *ctx, int argc, const uint16_t *arglens, const c
if (argc != 2) {
return -1;
}
return tf_func_longest(ctx, argc, arglens, args, out, outlen, fail_on_undef);

int bool_out = 0;

const char *arg = args;
int len;
TF_EVAL_CHECK(len, ctx, arg, arglens[0], out, outlen, fail_on_undef);
size_t l1 = u8_strlen (out);

arg += arglens[0];
TF_EVAL_CHECK(len, ctx, arg, arglens[1], out, outlen, fail_on_undef);
size_t l2 = u8_strlen (out);

return l1 > l2;
}

// $pad(expr,len[, char]): If `expr` is shorter than len characters, the function adds `char` characters (if present otherwise
Expand Down

0 comments on commit 614a94e

Please sign in to comment.