开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (5)
标签 (3)
main
feature/v6
test/test-most-core-func
version/i18n/zh-cn
feature/prod
v3
v2
v1
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
main
分支 (5)
标签 (3)
main
feature/v6
test/test-most-core-func
version/i18n/zh-cn
feature/prod
v3
v2
v1
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (5)
标签 (3)
main
feature/v6
test/test-most-core-func
version/i18n/zh-cn
feature/prod
v3
v2
v1
claude-code
/
src
/
utils
/
stringUtils.ts
claude-code
/
src
/
utils
/
stringUtils.ts
stringUtils.ts 6.44 KB
一键复制 编辑 原始数据 按行查看 历史
claude-code-best 提交于 2026年03月31日 19:22 +08:00 . feat: build
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/**
* General string utility functions and classes for safe string accumulation
*/
/**
* Escapes special regex characters in a string so it can be used as a literal
* pattern in a RegExp constructor.
*/
export function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
/**
* Uppercases the first character of a string, leaving the rest unchanged.
* Unlike lodash `capitalize`, this does NOT lowercase the remaining characters.
*
* @example capitalize('fooBar') → 'FooBar'
* @example capitalize('hello world') → 'Hello world'
*/
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1)
}
/**
* Returns the singular or plural form of a word based on count.
* Replaces the inline `word${n === 1 ? '' : 's'}` idiom.
*
* @example plural(1, 'file') → 'file'
* @example plural(3, 'file') → 'files'
* @example plural(2, 'entry', 'entries') → 'entries'
*/
export function plural(
n: number,
word: string,
pluralWord = word + 's',
): string {
return n === 1 ? word : pluralWord
}
/**
* Returns the first line of a string without allocating a split array.
* Used for shebang detection in diff rendering.
*/
export function firstLineOf(s: string): string {
const nl = s.indexOf('\n')
return nl === -1 ? s : s.slice(0, nl)
}
/**
* Counts occurrences of `char` in `str` using indexOf jumps instead of
* per-character iteration. Structurally typed so Buffer works too
* (Buffer.indexOf accepts string needles).
*/
export function countCharInString(
str: { indexOf(search: string, start?: number): number },
char: string,
start = 0,
): number {
let count = 0
let i = str.indexOf(char, start)
while (i !== -1) {
count++
i = str.indexOf(char, i + 1)
}
return count
}
/**
* Normalize full-width (zenkaku) digits to half-width digits.
* Useful for accepting input from Japanese/CJK IMEs.
*/
export function normalizeFullWidthDigits(input: string): string {
return input.replace(/[0-9]/g, ch =>
String.fromCharCode(ch.charCodeAt(0) - 0xfee0),
)
}
/**
* Normalize full-width (zenkaku) space to half-width space.
* Useful for accepting input from Japanese/CJK IMEs (U+3000 → U+0020).
*/
export function normalizeFullWidthSpace(input: string): string {
return input.replace(/\u3000/g, '')
}
// Keep in-memory accumulation modest to avoid blowing up RSS.
// Overflow beyond this limit is spilled to disk by ShellCommand.
const MAX_STRING_LENGTH = 2 ** 25
/**
* Safely joins an array of strings with a delimiter, truncating if the result exceeds maxSize.
*
* @param lines Array of strings to join
* @param delimiter Delimiter to use between strings (default: ',')
* @param maxSize Maximum size of the resulting string
* @returns The joined string, truncated if necessary
*/
export function safeJoinLines(
lines: string[],
delimiter: string = ',',
maxSize: number = MAX_STRING_LENGTH,
): string {
const truncationMarker = '...[truncated]'
let result = ''
for (const line of lines) {
const delimiterToAdd = result ? delimiter : ''
const fullAddition = delimiterToAdd + line
if (result.length + fullAddition.length <= maxSize) {
// The full line fits
result += fullAddition
} else {
// Need to truncate
const remainingSpace =
maxSize -
result.length -
delimiterToAdd.length -
truncationMarker.length
if (remainingSpace > 0) {
// Add delimiter and as much of the line as will fit
result +=
delimiterToAdd + line.slice(0, remainingSpace) + truncationMarker
} else {
// No room for any of this line, just add truncation marker
result += truncationMarker
}
return result
}
}
return result
}
/**
* A string accumulator that safely handles large outputs by truncating from the end
* when a size limit is exceeded. This prevents RangeError crashes while preserving
* the beginning of the output.
*/
export class EndTruncatingAccumulator {
private content: string = ''
private isTruncated = false
private totalBytesReceived = 0
/**
* Creates a new EndTruncatingAccumulator
* @param maxSize Maximum size in characters before truncation occurs
*/
constructor(private readonly maxSize: number = MAX_STRING_LENGTH) {}
/**
* Appends data to the accumulator. If the total size exceeds maxSize,
* the end is truncated to maintain the size limit.
* @param data The string data to append
*/
append(data: string | Buffer): void {
const str = typeof data === 'string' ? data : data.toString()
this.totalBytesReceived += str.length
// If already at capacity and truncated, don't modify content
if (this.isTruncated && this.content.length >= this.maxSize) {
return
}
// Check if adding the string would exceed the limit
if (this.content.length + str.length > this.maxSize) {
// Only append what we can fit
const remainingSpace = this.maxSize - this.content.length
if (remainingSpace > 0) {
this.content += str.slice(0, remainingSpace)
}
this.isTruncated = true
} else {
this.content += str
}
}
/**
* Returns the accumulated string, with truncation marker if truncated
*/
toString(): string {
if (!this.isTruncated) {
return this.content
}
const truncatedBytes = this.totalBytesReceived - this.maxSize
const truncatedKB = Math.round(truncatedBytes / 1024)
return this.content + `\n... [output truncated - ${truncatedKB}KB removed]`
}
/**
* Clears all accumulated data
*/
clear(): void {
this.content = ''
this.isTruncated = false
this.totalBytesReceived = 0
}
/**
* Returns the current size of accumulated data
*/
get length(): number {
return this.content.length
}
/**
* Returns whether truncation has occurred
*/
get truncated(): boolean {
return this.isTruncated
}
/**
* Returns total bytes received (before truncation)
*/
get totalBytes(): number {
return this.totalBytesReceived
}
}
/**
* Truncates text to a maximum number of lines, adding an ellipsis if truncated.
*
* @param text The text to truncate
* @param maxLines Maximum number of lines to keep
* @returns The truncated text with ellipsis if truncated
*/
export function truncateToLines(text: string, maxLines: number): string {
const lines = text.split('\n')
if (lines.length <= maxLines) {
return text
}
return lines.slice(0, maxLines).join('\n') + '...'
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jbcode/claude-code.git
git@gitee.com:jbcode/claude-code.git
jbcode
claude-code
claude-code
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /