Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
steam-runtime-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
steamrt
steam-runtime-tools
Commits
67daf3ab
Commit
67daf3ab
authored
8 months ago
by
Simon McVittie
Browse files
Options
Downloads
Patches
Plain Diff
utils: Add _srt_string_ends_with
Signed-off-by:
Simon McVittie
<
smcv@collabora.com
>
parent
b8875eff
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
steam-runtime-tools/utils-internal.h
+17
-0
17 additions, 0 deletions
steam-runtime-tools/utils-internal.h
tests/utils.c
+41
-0
41 additions, 0 deletions
tests/utils.c
with
58 additions
and
0 deletions
steam-runtime-tools/utils-internal.h
+
17
−
0
View file @
67daf3ab
...
...
@@ -603,3 +603,20 @@ goffset _srt_byte_suffix_to_multiplier (const char *suffix);
gboolean
_srt_string_read_fd_until_eof
(
GString
*
buf
,
int
fd
,
GError
**
error
);
/*
* _srt_string_ends_with:
* @str: A #GString
* @suffix: A suffix
*
* Returns: %TRUE if @str ends with @suffix
*/
static
inline
gboolean
_srt_string_ends_with
(
const
GString
*
str
,
const
char
*
suffix
)
{
size_t
len
=
strlen
(
suffix
);
return
(
str
->
len
>=
len
&&
strcmp
(
str
->
str
+
str
->
len
-
len
,
suffix
)
==
0
);
}
This diff is collapsed.
Click to expand it.
tests/utils.c
+
41
−
0
View file @
67daf3ab
...
...
@@ -1480,6 +1480,45 @@ test_str_is_integer (Fixture *f,
g_assert_false
(
_srt_str_is_integer
(
"23a"
));
}
static
const
struct
{
const
char
*
str
;
ssize_t
len
;
const
char
*
suffix
;
gboolean
expected
;
}
string_ends
[]
=
{
{
""
,
0
,
""
,
TRUE
},
{
"bar"
,
-
1
,
"bar"
,
TRUE
},
{
"foobar"
,
-
1
,
"bar"
,
TRUE
},
{
"foobar"
,
-
1
,
"BAR"
,
FALSE
},
{
"foo
\0
bar"
,
7
,
"ar"
,
TRUE
},
{
"foo
\0
bar"
,
7
,
"aa"
,
FALSE
},
};
static
void
test_string_ends_with
(
Fixture
*
f
,
gconstpointer
context
)
{
size_t
i
;
for
(
i
=
0
;
i
<
G_N_ELEMENTS
(
string_ends
);
i
++
)
{
g_autoptr
(
GString
)
str
=
NULL
;
gboolean
result
;
str
=
g_string_new_len
(
string_ends
[
i
].
str
,
string_ends
[
i
].
len
);
result
=
_srt_string_ends_with
(
str
,
string_ends
[
i
].
suffix
);
g_test_message
(
"#%zu
\"
%s
\"
ends with
\"
%s
\"
: %c, expected: %c"
,
i
,
string_ends
[
i
].
len
<
0
?
string_ends
[
i
].
str
:
"<not null-terminated>"
,
string_ends
[
i
].
suffix
,
result
?
'y'
:
'n'
,
string_ends
[
i
].
expected
?
'y'
:
'n'
);
g_assert_cmpint
(
result
,
==
,
string_ends
[
i
].
expected
);
}
}
static
void
test_string_read_fd_until_eof
(
Fixture
*
f
,
gconstpointer
context
)
...
...
@@ -1627,6 +1666,8 @@ main (int argc,
setup
,
test_same_file
,
teardown
);
g_test_add
(
"/utils/str_is_integer"
,
Fixture
,
NULL
,
setup
,
test_str_is_integer
,
teardown
);
g_test_add
(
"/utils/string_ends_with"
,
Fixture
,
NULL
,
setup
,
test_string_ends_with
,
teardown
);
g_test_add
(
"/utils/string_read_fd_until_eof"
,
Fixture
,
NULL
,
setup
,
test_string_read_fd_until_eof
,
teardown
);
g_test_add
(
"/utils/uevent-field"
,
Fixture
,
NULL
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment