Go中如何正确处理带有转义的字符串?

2024年 2月 5日 61.5k 0

go中如何正确处理带有转义的字符串?

问题内容

我正在创建一个程序,该程序正在处理和计算开源存储库和库的大小,并将数据保存到数据库中以供进一步分析。

  • 我有一个输入字符串:github.com/azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1
  • 解析为格式:github.com/!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1
  • 然后我将其解析为 /home/username/dev/glass/tmp/pkg/mod/github.com/!azure/[email protected] 格式,这是我的文件系统中的有效路径,我在其中已下载该特定的 go 库。
  • 之后,我将该路径传递给 gocloc -程序 (https://github.com/hhatto/gocloc)
  • 并解析结果。

但问题是,当我将字符串 /home/username/dev/glass/tmp/pkg/mod/github.com/!azure/[email protected] 保存到变量中时,go 实际上添加了对我保存的字符串的另一个转义,因此它实际上是内存中的 /home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/[email protected] 。 (fmt.println - 例如删除它)

问题是,当我将该字符串作为参数传递给 os/exec(运行 gocloc 和该路径字符串)时,它运行带有两个转义符的命令 - 这不是有效的路径。

有什么办法可以解决这个问题吗?对我来说,一个想法是只创建一个关于我想做的事情的 shell 脚本

此函数将 github.com/azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 解析为 github.com/!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a 格式1 - 及之后这被保存到一个变量中,并且该变量比它应该有的多了一次转义。

func parseurltovendordownloadformat(input string) string {
// split the input string on the first space character
parts := strings.splitn(input, " ", 2)
if len(parts) != 2 {
return ""
}

// split the package name on the '/' character
packagenameparts := strings.split(parts[0], "/")

// add the '!' prefix and lowercase each part of the package name
for i, part := range packagenameparts {
if hasuppercase(part) {
packagenameparts[i] = "\!" + strings.tolower(part)
}
}

// join the modified package name parts with '/' characters
packagename := strings.join(packagenameparts, "/")

return strings.replaceall(packagename+"@"+parts[1], `\!`, `!`)
}

登录后复制

之后,字符串被解析为以下格式:/home/username/dev/glass/tmp/pkg/mod/github.com/!azure/[email protected]

传递给此函数:

// alternative gocloc - command.
func linesofcode(dir string) (int, error) {
// run the `gocloc` command in the specified directory and get the output
cmd := exec.command("gocloc", dir)
output, err := cmd.output()
if err != nil {
return 0, err
}

lines, err := parsetotallines(string(output))
if err != nil {
return 0, err
}

return lines, nil
}

登录后复制

它使用这个解析函数:

// Parse from the GoCloc response.
func parseTotalLines(input string) (int, error) {
// Split the input string into lines
lines := strings.Split(input, "n")

// Find the line containing the "TOTAL" row
var totalLine string
for _, line := range lines {
if strings.Contains(line, "TOTAL") {
totalLine = line
break
}
}

// If the "TOTAL" line was not found, return an error
if totalLine == "" {
return 0, fmt.Errorf("could not find TOTAL line in input")
}

// Split the "TOTAL" line into fields
fields := strings.Fields(totalLine)

// If the "TOTAL" line doesn't have enough fields, return an error
if len(fields) 登录后复制

我尝试过的:

  • 使用 gocloc 作为库,但无法正常工作。
  • 使用单引号而不是转义符,没有让它工作,但我认为可能有一些东西。

解决这个问题的一种方法可能是创建单独的 shell 脚本并将目录作为参数传递给该脚本,并消除那里的转义,我不知道......

如果您想观察所有源代码:https://github.com/haapjari/glass,更具体地说,是文件 https://github.com/haapjari/glass/blob/main/pkg/plugins /goplg/plugin.go 和函数 enrichwithlibrarydata() 和 utils 函数,位于:https://github.com/haapjari/glass/blob/main/pkg/plugins/goplg/utils.go (上面的示例)

有什么想法吗?如何进行?提前致谢!

正确答案

我有一个输入字符串:github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

解析为格式:github.com/!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

您的解析器似乎有错误。我希望 Azure 成为 !azure:

github.com/!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

Go 模块参考

为了避免在不区分大小写的文件系统中提供服务时出现歧义,$module 和 $version 元素进行大小写编码,方法是将每个大写字母替换为感叹号,后跟相应的小写字母。这允许模块 example.com/mexample.com/m 都存储在磁盘上,因为前者被编码为 example.com/!m

以上就是Go中如何正确处理带有转义的字符串?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论