forked from datawhale/whale-town-front
chore:完善项目目录结构和基础框架
- 添加核心系统框架目录结构 - 创建游戏模块化组织架构 - 添加数据配置和本地化支持 - 建立脚本分类管理体系 - 创建场景预制件管理目录
This commit is contained in:
105
core/utils/StringUtils.gd
Normal file
105
core/utils/StringUtils.gd
Normal file
@@ -0,0 +1,105 @@
|
||||
class_name StringUtils
|
||||
|
||||
# 字符串工具类 - 提供常用的字符串处理功能
|
||||
|
||||
# 验证邮箱格式
|
||||
static func is_valid_email(email: String) -> bool:
|
||||
var regex = RegEx.new()
|
||||
regex.compile("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")
|
||||
return regex.search(email) != null
|
||||
|
||||
# 验证用户名格式(字母、数字、下划线)
|
||||
static func is_valid_username(username: String) -> bool:
|
||||
if username.is_empty() or username.length() > 50:
|
||||
return false
|
||||
|
||||
var regex = RegEx.new()
|
||||
regex.compile("^[a-zA-Z0-9_]+$")
|
||||
return regex.search(username) != null
|
||||
|
||||
# 验证密码强度
|
||||
static func validate_password_strength(password: String) -> Dictionary:
|
||||
var result = {"valid": false, "message": "", "strength": 0}
|
||||
|
||||
if password.length() < 8:
|
||||
result.message = "密码长度至少8位"
|
||||
return result
|
||||
|
||||
if password.length() > 128:
|
||||
result.message = "密码长度不能超过128位"
|
||||
return result
|
||||
|
||||
var has_letter = false
|
||||
var has_digit = false
|
||||
var has_special = false
|
||||
|
||||
for i in range(password.length()):
|
||||
var char = password[i]
|
||||
if char >= 'a' and char <= 'z' or char >= 'A' and char <= 'Z':
|
||||
has_letter = true
|
||||
elif char >= '0' and char <= '9':
|
||||
has_digit = true
|
||||
elif char in "!@#$%^&*()_+-=[]{}|;:,.<>?":
|
||||
has_special = true
|
||||
|
||||
var strength = 0
|
||||
if has_letter:
|
||||
strength += 1
|
||||
if has_digit:
|
||||
strength += 1
|
||||
if has_special:
|
||||
strength += 1
|
||||
if password.length() >= 12:
|
||||
strength += 1
|
||||
|
||||
result.strength = strength
|
||||
|
||||
if not (has_letter and has_digit):
|
||||
result.message = "密码必须包含字母和数字"
|
||||
return result
|
||||
|
||||
result.valid = true
|
||||
result.message = "密码强度: " + ["弱", "中", "强", "很强"][min(strength - 1, 3)]
|
||||
return result
|
||||
|
||||
# 截断字符串
|
||||
static func truncate(text: String, max_length: int, suffix: String = "...") -> String:
|
||||
if text.length() <= max_length:
|
||||
return text
|
||||
return text.substr(0, max_length - suffix.length()) + suffix
|
||||
|
||||
# 首字母大写
|
||||
static func capitalize_first(text: String) -> String:
|
||||
if text.is_empty():
|
||||
return text
|
||||
return text[0].to_upper() + text.substr(1)
|
||||
|
||||
# 转换为标题格式(每个单词首字母大写)
|
||||
static func to_title_case(text: String) -> String:
|
||||
var words = text.split(" ")
|
||||
var result = []
|
||||
for word in words:
|
||||
if not word.is_empty():
|
||||
result.append(capitalize_first(word.to_lower()))
|
||||
return " ".join(result)
|
||||
|
||||
# 移除HTML标签
|
||||
static func strip_html_tags(html: String) -> String:
|
||||
var regex = RegEx.new()
|
||||
regex.compile("<[^>]*>")
|
||||
return regex.sub(html, "", true)
|
||||
|
||||
# 格式化文件大小
|
||||
static func format_file_size(bytes: int) -> String:
|
||||
var units = ["B", "KB", "MB", "GB", "TB"]
|
||||
var size = float(bytes)
|
||||
var unit_index = 0
|
||||
|
||||
while size >= 1024.0 and unit_index < units.size() - 1:
|
||||
size /= 1024.0
|
||||
unit_index += 1
|
||||
|
||||
if unit_index == 0:
|
||||
return str(int(size)) + " " + units[unit_index]
|
||||
else:
|
||||
return "%.1f %s" % [size, units[unit_index]]
|
||||
Reference in New Issue
Block a user