Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1c01f8d

Browse files
committed
Add fl_value_equal_string()
For comparing an FlValue to a string (constant) without having to allocate another FlValue just for the comparison. This helps to fix a memory leak in FlTextInputPlugin.
1 parent 6cdb2f6 commit 1c01f8d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

shell/platform/linux/fl_value.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ G_MODULE_EXPORT bool fl_value_equal(FlValue* a, FlValue* b) {
503503
}
504504
}
505505

506+
G_MODULE_EXPORT bool fl_value_equal_string(FlValue* self, const gchar* value) {
507+
g_return_val_if_fail(self != nullptr, false);
508+
g_return_val_if_fail(value != nullptr, false);
509+
g_return_val_if_fail(self->type == FL_VALUE_TYPE_STRING, false);
510+
511+
FlValueString* str_ = reinterpret_cast<FlValueString*>(self);
512+
return g_strcmp0(str_->value, value) == 0;
513+
}
514+
506515
G_MODULE_EXPORT void fl_value_append(FlValue* self, FlValue* value) {
507516
g_return_if_fail(self != nullptr);
508517
g_return_if_fail(self->type == FL_VALUE_TYPE_LIST);

shell/platform/linux/fl_value_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,16 @@ TEST(FlValueTest, StringEqual) {
185185
g_autoptr(FlValue) value1 = fl_value_new_string("hello");
186186
g_autoptr(FlValue) value2 = fl_value_new_string("hello");
187187
EXPECT_TRUE(fl_value_equal(value1, value2));
188+
EXPECT_TRUE(fl_value_equal_string(value1, "hello"));
189+
EXPECT_TRUE(fl_value_equal_string(value2, "hello"));
188190
}
189191

190192
TEST(FlValueTest, StringNotEqual) {
191193
g_autoptr(FlValue) value1 = fl_value_new_string("hello");
192194
g_autoptr(FlValue) value2 = fl_value_new_string("world");
193195
EXPECT_FALSE(fl_value_equal(value1, value2));
196+
EXPECT_FALSE(fl_value_equal_string(value1, "world"));
197+
EXPECT_FALSE(fl_value_equal_string(value2, "hello"));
194198
}
195199

196200
TEST(FlValueTest, StringToString) {

shell/platform/linux/public/flutter_linux/fl_value.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,19 @@ FlValueType fl_value_get_type(FlValue* value);
312312
*/
313313
bool fl_value_equal(FlValue* a, FlValue* b);
314314

315+
/**
316+
* fl_value_equal_string:
317+
* @value: an #FlValue.
318+
* @string: a %NULL-terminated UTF-8 string.
319+
*
320+
* Compares an #FlValue to a @string to see if they are equivalent. Calling
321+
* this with an #FlValue that is not of type #FL_VALUE_TYPE_STRING is a
322+
* programming error.
323+
*
324+
* Returns: %TRUE if the strings are equivalent.
325+
*/
326+
bool fl_value_equal_string(FlValue* value, const gchar* string);
327+
315328
/**
316329
* fl_value_append:
317330
* @value: an #FlValue of type #FL_VALUE_TYPE_LIST.

0 commit comments

Comments
 (0)