Skip to content

Commit d89ff19

Browse files
authored
GH-48509: [GLib][Ruby] Add TrimOptions (#48530)
### Rationale for this change The `TrimOptions` class is not available in GLib/Ruby, and it is used together with the `utf8_trim` compute function. ### What changes are included in this PR? This adds the `TrimOptions` class to GLib. ### Are these changes tested? Yes, with Ruby unit tests. ### Are there any user-facing changes? Yes, a new class. * GitHub Issue: #48509 Authored-by: Sten Larsson <sten@burtcorp.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 0b8fb56 commit d89ff19

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ G_BEGIN_DECLS
329329
* #GArrowTDigestOptions is a class to customize the `tdigest` and
330330
* `hash_tdigest` functions.
331331
*
332+
* #GArrowTrimOptions is a class to customize the `utf8_trim`, `utf8_ltrim`,
333+
* `utf8_rtrim`, `ascii_trim`, `ascii_ltrim`, and `ascii_rtrim` functions.
334+
*
332335
* There are many functions to compute data on an array.
333336
*/
334337

@@ -9996,6 +9999,95 @@ garrow_tdigest_options_set_qs(GArrowTDigestOptions *options, const gdouble *qs,
99969999
}
999710000
}
999810001

10002+
enum {
10003+
PROP_TRIM_OPTIONS_CHARACTERS = 1,
10004+
};
10005+
10006+
G_DEFINE_TYPE(GArrowTrimOptions, garrow_trim_options, GARROW_TYPE_FUNCTION_OPTIONS)
10007+
10008+
static void
10009+
garrow_trim_options_set_property(GObject *object,
10010+
guint prop_id,
10011+
const GValue *value,
10012+
GParamSpec *pspec)
10013+
{
10014+
auto options = garrow_trim_options_get_raw(GARROW_TRIM_OPTIONS(object));
10015+
10016+
switch (prop_id) {
10017+
case PROP_TRIM_OPTIONS_CHARACTERS:
10018+
options->characters = g_value_get_string(value);
10019+
break;
10020+
default:
10021+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10022+
break;
10023+
}
10024+
}
10025+
10026+
static void
10027+
garrow_trim_options_get_property(GObject *object,
10028+
guint prop_id,
10029+
GValue *value,
10030+
GParamSpec *pspec)
10031+
{
10032+
auto options = garrow_trim_options_get_raw(GARROW_TRIM_OPTIONS(object));
10033+
10034+
switch (prop_id) {
10035+
case PROP_TRIM_OPTIONS_CHARACTERS:
10036+
g_value_set_string(value, options->characters.c_str());
10037+
break;
10038+
default:
10039+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10040+
break;
10041+
}
10042+
}
10043+
10044+
static void
10045+
garrow_trim_options_init(GArrowTrimOptions *object)
10046+
{
10047+
auto arrow_priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10048+
arrow_priv->options =
10049+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::TrimOptions());
10050+
}
10051+
10052+
static void
10053+
garrow_trim_options_class_init(GArrowTrimOptionsClass *klass)
10054+
{
10055+
auto gobject_class = G_OBJECT_CLASS(klass);
10056+
10057+
gobject_class->set_property = garrow_trim_options_set_property;
10058+
gobject_class->get_property = garrow_trim_options_get_property;
10059+
10060+
arrow::compute::TrimOptions options;
10061+
10062+
GParamSpec *spec;
10063+
/**
10064+
* GArrowTrimOptions:characters:
10065+
*
10066+
* The individual characters to be trimmed from the string.
10067+
*
10068+
* Since: 23.0.0
10069+
*/
10070+
spec = g_param_spec_string("characters",
10071+
"Characters",
10072+
"The individual characters to be trimmed from the string",
10073+
options.characters.c_str(),
10074+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10075+
g_object_class_install_property(gobject_class, PROP_TRIM_OPTIONS_CHARACTERS, spec);
10076+
}
10077+
10078+
/**
10079+
* garrow_trim_options_new:
10080+
*
10081+
* Returns: A newly created #GArrowTrimOptions.
10082+
*
10083+
* Since: 23.0.0
10084+
*/
10085+
GArrowTrimOptions *
10086+
garrow_trim_options_new(void)
10087+
{
10088+
return GARROW_TRIM_OPTIONS(g_object_new(GARROW_TYPE_TRIM_OPTIONS, nullptr));
10089+
}
10090+
999910091
G_END_DECLS
1000010092

1000110093
arrow::Result<arrow::FieldRef>
@@ -10256,6 +10348,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1025610348
static_cast<const arrow::compute::TDigestOptions *>(arrow_options);
1025710349
auto options = garrow_tdigest_options_new_raw(arrow_tdigest_options);
1025810350
return GARROW_FUNCTION_OPTIONS(options);
10351+
} else if (arrow_type_name == "TrimOptions") {
10352+
const auto arrow_trim_options =
10353+
static_cast<const arrow::compute::TrimOptions *>(arrow_options);
10354+
auto options = garrow_trim_options_new_raw(arrow_trim_options);
10355+
return GARROW_FUNCTION_OPTIONS(options);
1025910356
} else {
1026010357
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1026110358
return GARROW_FUNCTION_OPTIONS(options);
@@ -11317,3 +11414,19 @@ garrow_tdigest_options_get_raw(GArrowTDigestOptions *options)
1131711414
return static_cast<arrow::compute::TDigestOptions *>(
1131811415
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1131911416
}
11417+
11418+
GArrowTrimOptions *
11419+
garrow_trim_options_new_raw(const arrow::compute::TrimOptions *arrow_options)
11420+
{
11421+
return GARROW_TRIM_OPTIONS(g_object_new(GARROW_TYPE_TRIM_OPTIONS,
11422+
"characters",
11423+
arrow_options->characters.c_str(),
11424+
nullptr));
11425+
}
11426+
11427+
arrow::compute::TrimOptions *
11428+
garrow_trim_options_get_raw(GArrowTrimOptions *options)
11429+
{
11430+
return static_cast<arrow::compute::TrimOptions *>(
11431+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
11432+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,4 +1729,17 @@ GARROW_AVAILABLE_IN_23_0
17291729
void
17301730
garrow_tdigest_options_set_qs(GArrowTDigestOptions *options, const gdouble *qs, gsize n);
17311731

1732+
#define GARROW_TYPE_TRIM_OPTIONS (garrow_trim_options_get_type())
1733+
GARROW_AVAILABLE_IN_23_0
1734+
G_DECLARE_DERIVABLE_TYPE(
1735+
GArrowTrimOptions, garrow_trim_options, GARROW, TRIM_OPTIONS, GArrowFunctionOptions)
1736+
struct _GArrowTrimOptionsClass
1737+
{
1738+
GArrowFunctionOptionsClass parent_class;
1739+
};
1740+
1741+
GARROW_AVAILABLE_IN_23_0
1742+
GArrowTrimOptions *
1743+
garrow_trim_options_new(void);
1744+
17321745
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,8 @@ GArrowTDigestOptions *
318318
garrow_tdigest_options_new_raw(const arrow::compute::TDigestOptions *arrow_options);
319319
arrow::compute::TDigestOptions *
320320
garrow_tdigest_options_get_raw(GArrowTDigestOptions *options);
321+
322+
GArrowTrimOptions *
323+
garrow_trim_options_new_raw(const arrow::compute::TrimOptions *arrow_options);
324+
arrow::compute::TrimOptions *
325+
garrow_trim_options_get_raw(GArrowTrimOptions *options);

c_glib/test/test-trim-options.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestTrimOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::TrimOptions.new
23+
end
24+
25+
def test_characters_property
26+
assert_equal("", @options.characters)
27+
@options.characters = " \t"
28+
assert_equal(" \t", @options.characters)
29+
end
30+
31+
def test_utf8_trim_function
32+
args = [
33+
Arrow::ArrayDatum.new(build_string_array([" hello ", " world "])),
34+
]
35+
@options.characters = " "
36+
utf8_trim_function = Arrow::Function.find("utf8_trim")
37+
result = utf8_trim_function.execute(args, @options).value
38+
expected = build_string_array(["hello", "world"])
39+
assert_equal(expected, result)
40+
end
41+
end

0 commit comments

Comments
 (0)