Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cta
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Harbor 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
dCache
cta
Commits
1b6ae54b
Commit
1b6ae54b
authored
8 years ago
by
Eric Cano
Browse files
Options
Downloads
Patches
Plain Diff
Added utils functions allowing ellipsing strings when too long
The targetted use case is truncation of long log parameters.
parent
cf227d94
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
common/utils/UtilsTest.cpp
+10
-0
10 additions, 0 deletions
common/utils/UtilsTest.cpp
common/utils/utils.cpp
+26
-0
26 additions, 0 deletions
common/utils/utils.cpp
common/utils/utils.hpp
+24
-1
24 additions, 1 deletion
common/utils/utils.hpp
with
60 additions
and
1 deletion
common/utils/UtilsTest.cpp
+
10
−
0
View file @
1b6ae54b
...
...
@@ -648,4 +648,14 @@ TEST_F(cta_UtilsTest, testCopyString) {
ASSERT_EQ
(
0
,
strcmp
(
dst
,
src
));
}
TEST_F
(
cta_UtilsTest
,
ellipses
)
{
using
namespace
cta
::
utils
;
ASSERT_EQ
(
"1234567890"
,
postEllipsis
(
"1234567890"
,
12
));
ASSERT_EQ
(
"1234567[...]"
,
postEllipsis
(
"1234567890ABCDEF"
,
12
));
ASSERT_EQ
(
"1234567890"
,
midEllipsis
(
"1234567890"
,
12
));
ASSERT_EQ
(
"123[...]CDEF"
,
midEllipsis
(
"1234567890ABCDEF"
,
12
));
ASSERT_EQ
(
"1[...]ABCDEF"
,
midEllipsis
(
"1234567890ABCDEF"
,
12
,
1
));
}
}
// namespace unitTests
This diff is collapsed.
Click to expand it.
common/utils/utils.cpp
+
26
−
0
View file @
1b6ae54b
...
...
@@ -259,6 +259,32 @@ std::string trimString(const std::string &s) throw() {
return
std
::
string
(
it1
,
it2
);
}
//------------------------------------------------------------------------------
// postEllipsis
//------------------------------------------------------------------------------
std
::
string
postEllipsis
(
const
std
::
string
&
s
,
size_t
maxSize
)
{
std
::
string
ellipsis
=
"[...]"
;
if
(
maxSize
<
ellipsis
.
size
())
throw
cta
::
exception
::
Exception
(
"In cta::utils::postEllipsis(): maxSize cannot be smaller than ellipsis size"
);
if
(
s
.
size
()
<=
maxSize
)
return
s
;
return
s
.
substr
(
0
,
maxSize
-
ellipsis
.
size
())
+
ellipsis
;
}
//------------------------------------------------------------------------------
// midEllipsis
//------------------------------------------------------------------------------
std
::
string
midEllipsis
(
const
std
::
string
&
s
,
size_t
maxSize
,
size_t
beginingSize
)
{
std
::
string
ellipsis
=
"[...]"
;
if
(
maxSize
<
ellipsis
.
size
()
+
beginingSize
)
throw
cta
::
exception
::
Exception
(
"In cta::utils::midEllipsis(): maxSize cannot be smaller than ellipsis size + beginingSize"
);
if
(
s
.
size
()
<=
maxSize
)
return
s
;
if
(
!
beginingSize
)
beginingSize
=
(
maxSize
-
ellipsis
.
size
())
/
2
;
return
s
.
substr
(
0
,
beginingSize
)
+
ellipsis
+
s
.
substr
(
s
.
size
()
-
maxSize
+
ellipsis
.
size
()
+
beginingSize
);
}
//------------------------------------------------------------------------------
// singleSpaceString
//------------------------------------------------------------------------------
...
...
This diff is collapsed.
Click to expand it.
common/utils/utils.hpp
+
24
−
1
View file @
1b6ae54b
...
...
@@ -109,8 +109,31 @@ namespace utils {
* @param s The string to be trimmed.
* @return The result of trimming the string.
*/
std
::
string
trimString
(
const
std
::
string
&
s
)
throw
();
std
::
string
trimString
(
const
std
::
string
&
s
)
noexcept
;
/**
* Returns a string with an ellipsis in the end if necessary so that the
* string plus ellipsis does not exceed the maxSize. The returned string is
* identical to s if it fits the maximum size.
* @param s the string
* @param maxSize
* @return the ellipsed string
*/
std
::
string
postEllipsis
(
const
std
::
string
&
s
,
size_t
maxSize
);
/**
* Returns a string with an ellipsis in the middle if necessary so that the
* string plus ellipsis does not exceed the maxSize. The returned string is
* identical to s if it fits the maximum size. The parts before and after the
* ellipsis are evenly distributed, unless beginingSize is set to a non zero
* value.
* @param s the string
* @param maxSize
* @param beginingSize
* @return the ellipsed string
*/
std
::
string
midEllipsis
(
const
std
::
string
&
s
,
size_t
maxSize
,
size_t
beginingSize
=
0
);
/**
* Returns uuid in the form of a string.
*
...
...
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