Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
libvirt
提交
b475a91b
L
libvirt
项目概览
openeuler
/
libvirt
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
libvirt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b475a91b
编写于
12月 18, 2017
作者:
C
Cédric Bosdonnat
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add virStringFilterChars() string utility
Add a function to filter a string based on a list of valid characters.
上级
9026d115
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
72 addition
and
0 deletion
+72
-0
src/libvirt_private.syms
src/libvirt_private.syms
+1
-0
src/util/virstring.c
src/util/virstring.c
+24
-0
src/util/virstring.h
src/util/virstring.h
+1
-0
tests/virstringtest.c
tests/virstringtest.c
+46
-0
未找到文件。
src/libvirt_private.syms
浏览文件 @
b475a91b
...
...
@@ -2755,6 +2755,7 @@ virStrcpy;
virStrdup;
virStringBufferIsPrintable;
virStringEncodeBase64;
virStringFilterChars;
virStringHasChars;
virStringHasControlChars;
virStringIsEmpty;
...
...
src/util/virstring.c
浏览文件 @
b475a91b
...
...
@@ -1293,6 +1293,30 @@ virStringStripControlChars(char *str)
str
[
j
]
=
'\0'
;
}
/**
* virStringFilterChars:
* @str: the string to strip
* @valid: the valid characters for the string
*
* Modify the string in-place to remove the characters that aren't
* in the list of valid ones.
*/
void
virStringFilterChars
(
char
*
str
,
const
char
*
valid
)
{
size_t
len
,
i
,
j
;
if
(
!
str
)
return
;
len
=
strlen
(
str
);
for
(
i
=
0
,
j
=
0
;
i
<
len
;
i
++
)
{
if
(
strchr
(
valid
,
str
[
i
]))
str
[
j
++
]
=
str
[
i
];
}
str
[
j
]
=
'\0'
;
}
/**
* virStringToUpper:
* @str: string to capitalize
...
...
src/util/virstring.h
浏览文件 @
b475a91b
...
...
@@ -293,6 +293,7 @@ bool virStringHasChars(const char *str,
const
char
*
chars
);
bool
virStringHasControlChars
(
const
char
*
str
);
void
virStringStripControlChars
(
char
*
str
);
void
virStringFilterChars
(
char
*
str
,
const
char
*
valid
);
bool
virStringIsPrintable
(
const
char
*
str
);
bool
virStringBufferIsPrintable
(
const
uint8_t
*
buf
,
size_t
buflen
);
...
...
tests/virstringtest.c
浏览文件 @
b475a91b
...
...
@@ -767,6 +767,36 @@ static int testStripControlChars(const void *args)
return
ret
;
}
struct
testFilterData
{
const
char
*
string
;
const
char
*
valid
;
const
char
*
result
;
};
static
int
testFilterChars
(
const
void
*
args
)
{
const
struct
testFilterData
*
data
=
args
;
int
ret
=
-
1
;
char
*
res
=
NULL
;
if
(
VIR_STRDUP
(
res
,
data
->
string
)
<
0
)
goto
cleanup
;
virStringFilterChars
(
res
,
data
->
valid
);
if
(
STRNEQ_NULLABLE
(
res
,
data
->
result
))
{
fprintf
(
stderr
,
"Returned '%s', expected '%s'
\n
"
,
NULLSTR
(
res
),
NULLSTR
(
data
->
result
));
goto
cleanup
;
}
ret
=
0
;
cleanup:
VIR_FREE
(
res
);
return
ret
;
}
static
int
mymain
(
void
)
{
...
...
@@ -1085,6 +1115,22 @@ mymain(void)
TEST_STRIP_CONTROL_CHARS
(
"
\x01
H
\x02
"
"E
\x03
L
\x04
L
\x05
O"
,
"HELLO"
);
TEST_STRIP_CONTROL_CHARS
(
"
\x01\x02\x03\x04
HELL
\x05
O"
,
"HELLO"
);
TEST_STRIP_CONTROL_CHARS
(
"
\n
hello
\x01\x07
hello
\t
"
,
"
\n
hello hello
\t
"
);
#define TEST_FILTER_CHARS(str, filter, res) \
do { \
struct testFilterData filterData = { \
.string = str, \
.valid = filter, \
.result = res, \
}; \
if (virTestRun("Filter chars from " #str, \
testFilterChars, &filterData) < 0) \
ret = -1; \
} while (0)
TEST_FILTER_CHARS
(
NULL
,
NULL
,
NULL
);
TEST_FILTER_CHARS
(
"hello 123 hello"
,
"helo"
,
"hellohello"
);
return
ret
==
0
?
EXIT_SUCCESS
:
EXIT_FAILURE
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录