gpt4 book ai didi

c# - 代码审查 : CLR RegexSubstring

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:14 25 4
gpt4 key购买 nike

这会更好吗? .NET 2.0 与 SQL Server 2005 的兼容性:

public static SqlString RegexSubstring(SqlString regexpattern, 
SqlString sourcetext,
SqlInt32 start_position)
{
SqlString result = null;

if (!regexpattern.IsNull && !sourcetext.IsNull && !start_position.IsNull)
{
int start_location = (int)start_position >= 0 ? (int)start_position : 0;

Regex RegexInstance = new Regex(regexpattern.ToString());
result = new SqlString(RegexInstance.Match(sourcetext.ToString(),
start_location).Value);
}

return result;
}

这是我第一次尝试为 SQL Server 编写 CLR 函数/等 - 是否绝对有必要使用 SqlString/等数据类型作为参数?

最佳答案

只需通过 Refactor/Pro 运行它

给了这个:

public static SqlString RegexSubstring(SqlString regexpattern,
SqlString sourcetext,
SqlInt32 start_position) {
if (regexpattern.IsNull || sourcetext.IsNull || start_position.IsNull)
return null;

Regex RegexInstance = new Regex(regexpattern.ToString());

return new SqlString(RegexInstance.Match(sourcetext.ToString(),
(int)start_position).Value);
}

注意 start_location 未使用,所以您可能忽略了警告?

另一件事只是样式问题,但是可以将函数编写为不依赖于 SqtTypes 吗?那么代码就变成了:

    private static string RegexSubstring(string regexpattern, string sourcetext, int start_position) {

if (regexpattern == null || sourcetext == null || start_position == null)
return null;

Regex RegexInstance = new Regex(regexpattern);
return RegexInstance.Match(sourcetext, start_position).Value;
}

并用 :

调用它
new SqlString(RegexSubstring(regexpattern.ToString(), sourcetext.ToString(), start_position))

关于c# - 代码审查 : CLR RegexSubstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2822800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com