-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Add nodes for basic string operations #7952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,331 @@ | ||||||||||||||||
import re | ||||||||||||||||
|
||||||||||||||||
from comfy.comfy_types.node_typing import IO | ||||||||||||||||
|
||||||||||||||||
def normalize_path(path): | ||||||||||||||||
return path.replace('\\', '/') | ||||||||||||||||
|
||||||||||||||||
class StringConcatenate(): | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if the separator here is really necessary given we only support concat of 2 other strings. Can you find a reference of how existing node packs is handling the variable length input stacking and do that instead? Ref: There is variable length input proposed in Comfy-Org/rfcs#18, but currently it is not implemented yet. |
||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string_a": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"string_b": (IO.STRING, {"multiline": True}) | ||||||||||||||||
}, | ||||||||||||||||
"optional": { | ||||||||||||||||
"separator": (IO.STRING, {"default": ""}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.STRING,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string_a, string_b, separator="", **kwargs): | ||||||||||||||||
return string_a + separator + string_b, | ||||||||||||||||
|
||||||||||||||||
class StringSubstring(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"start": (IO.INT, {}), | ||||||||||||||||
"end": (IO.INT, {}), | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.STRING,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, start, end, **kwargs): | ||||||||||||||||
return string[start:end], | ||||||||||||||||
|
||||||||||||||||
class StringLength(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.INT,) | ||||||||||||||||
RETURN_NAMES = ("length",) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, **kwargs): | ||||||||||||||||
length = len(string) | ||||||||||||||||
|
||||||||||||||||
return length, | ||||||||||||||||
|
||||||||||||||||
class CaseConverter(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"mode": (["UPPERCASE", "lowercase", "Capitalize", "Title Case"], {}) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you convert the mode syntax to the preferred combo syntax described in ComfyUI/comfy/comfy_types/node_typing.py Lines 166 to 172 in 16417b4
|
||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.STRING,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, mode, **kwargs): | ||||||||||||||||
if mode == "UPPERCASE": | ||||||||||||||||
result = string.upper() | ||||||||||||||||
elif mode == "lowercase": | ||||||||||||||||
result = string.lower() | ||||||||||||||||
elif mode == "Capitalize": | ||||||||||||||||
result = string.capitalize() | ||||||||||||||||
elif mode == "Title Case": | ||||||||||||||||
result = string.title() | ||||||||||||||||
else: | ||||||||||||||||
result = string | ||||||||||||||||
|
||||||||||||||||
return result, | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class StringTrim(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"mode": (["BOTH", "LEFT", "RIGHT"], {}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.STRING,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, mode, **kwargs): | ||||||||||||||||
if mode == "BOTH": | ||||||||||||||||
result = string.strip() | ||||||||||||||||
elif mode == "LEFT": | ||||||||||||||||
result = string.lstrip() | ||||||||||||||||
elif mode == "RIGHT": | ||||||||||||||||
result = string.rstrip() | ||||||||||||||||
else: | ||||||||||||||||
result = string | ||||||||||||||||
|
||||||||||||||||
return result, | ||||||||||||||||
|
||||||||||||||||
class StringReplace(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"find": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"replace": (IO.STRING, {"multiline": True}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.STRING,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, find, replace, **kwargs): | ||||||||||||||||
result = string.replace(find, replace) | ||||||||||||||||
return result, | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class StringContains(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"substring": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"case_sensitive": (IO.BOOLEAN, {"default": True}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.BOOLEAN,) | ||||||||||||||||
RETURN_NAMES = ("contains",) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, substring, case_sensitive, **kwargs): | ||||||||||||||||
if case_sensitive: | ||||||||||||||||
contains = substring in string | ||||||||||||||||
else: | ||||||||||||||||
contains = substring.lower() in string.lower() | ||||||||||||||||
|
||||||||||||||||
return contains, | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class StringCompare(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string_a": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"string_b": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"mode": (["StartsWith", "EndsWith", "Equal"], {"default": "StartsWith"}), | ||||||||||||||||
"case_sensitive": (IO.BOOLEAN, {"default": True}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.BOOLEAN,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string_a, string_b, mode, case_sensitive, **kwargs): | ||||||||||||||||
if case_sensitive: | ||||||||||||||||
a = string_a | ||||||||||||||||
b = string_b | ||||||||||||||||
else: | ||||||||||||||||
a = string_a.lower() | ||||||||||||||||
b = string_b.lower() | ||||||||||||||||
|
||||||||||||||||
if mode == "Equal": | ||||||||||||||||
return a == b, | ||||||||||||||||
elif mode == "StartsWith": | ||||||||||||||||
return a.startswith(b), | ||||||||||||||||
elif mode == "EndsWith": | ||||||||||||||||
return a.endswith(b), | ||||||||||||||||
|
||||||||||||||||
class RegexMatch(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"regex_pattern": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"case_insensitive": (IO.BOOLEAN, {"default": True}), | ||||||||||||||||
"multiline": (IO.BOOLEAN, {"default": False}), | ||||||||||||||||
"dotall": (IO.BOOLEAN, {"default": False}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.BOOLEAN,) | ||||||||||||||||
RETURN_NAMES = ("matches",) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, regex_pattern, case_insensitive, | ||||||||||||||||
multiline, dotall, **kwargs): | ||||||||||||||||
flags = 0 | ||||||||||||||||
|
||||||||||||||||
if case_insensitive: | ||||||||||||||||
flags |= re.IGNORECASE | ||||||||||||||||
if multiline: | ||||||||||||||||
flags |= re.MULTILINE | ||||||||||||||||
if dotall: | ||||||||||||||||
flags |= re.DOTALL | ||||||||||||||||
|
||||||||||||||||
try: | ||||||||||||||||
match = re.search(regex_pattern, string, flags) | ||||||||||||||||
result = match is not None | ||||||||||||||||
|
||||||||||||||||
except re.error: | ||||||||||||||||
result = False | ||||||||||||||||
|
||||||||||||||||
return result, | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class RegexExtract(): | ||||||||||||||||
@classmethod | ||||||||||||||||
def INPUT_TYPES(s): | ||||||||||||||||
return { | ||||||||||||||||
"required": { | ||||||||||||||||
"string": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"regex_pattern": (IO.STRING, {"multiline": True}), | ||||||||||||||||
"mode": (["First Match", "All Matches", "First Group", "All Groups"], {}), | ||||||||||||||||
"case_insensitive": (IO.BOOLEAN, {"default": True}), | ||||||||||||||||
"multiline": (IO.BOOLEAN, {"default": False}), | ||||||||||||||||
"dotall": (IO.BOOLEAN, {"default": False}), | ||||||||||||||||
"group_index": (IO.INT, {"default": 1, "min": 0, "max": 100}) | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
RETURN_TYPES = (IO.STRING,) | ||||||||||||||||
FUNCTION = "execute" | ||||||||||||||||
CATEGORY = "utils/string" | ||||||||||||||||
|
||||||||||||||||
def execute(self, string, regex_pattern, mode, case_insensitive, | ||||||||||||||||
multiline, dotall, group_index, **kwargs): | ||||||||||||||||
|
||||||||||||||||
join_delimiter = "\n" | ||||||||||||||||
|
||||||||||||||||
flags = 0 | ||||||||||||||||
if case_insensitive: | ||||||||||||||||
flags |= re.IGNORECASE | ||||||||||||||||
if multiline: | ||||||||||||||||
flags |= re.MULTILINE | ||||||||||||||||
if dotall: | ||||||||||||||||
flags |= re.DOTALL | ||||||||||||||||
|
||||||||||||||||
try: | ||||||||||||||||
if mode == "First Match": | ||||||||||||||||
match = re.search(regex_pattern, string, flags) | ||||||||||||||||
if match: | ||||||||||||||||
result = match.group(0) | ||||||||||||||||
else: | ||||||||||||||||
result = "" | ||||||||||||||||
|
||||||||||||||||
elif mode == "All Matches": | ||||||||||||||||
matches = re.findall(regex_pattern, string, flags) | ||||||||||||||||
if matches: | ||||||||||||||||
if isinstance(matches[0], tuple): | ||||||||||||||||
result = join_delimiter.join([m[0] for m in matches]) | ||||||||||||||||
else: | ||||||||||||||||
result = join_delimiter.join(matches) | ||||||||||||||||
else: | ||||||||||||||||
result = "" | ||||||||||||||||
|
||||||||||||||||
elif mode == "First Group": | ||||||||||||||||
match = re.search(regex_pattern, string, flags) | ||||||||||||||||
if match and len(match.groups()) >= group_index: | ||||||||||||||||
result = match.group(group_index) | ||||||||||||||||
else: | ||||||||||||||||
result = "" | ||||||||||||||||
|
||||||||||||||||
elif mode == "All Groups": | ||||||||||||||||
matches = re.finditer(regex_pattern, string, flags) | ||||||||||||||||
results = [] | ||||||||||||||||
for match in matches: | ||||||||||||||||
if match.groups() and len(match.groups()) >= group_index: | ||||||||||||||||
results.append(match.group(group_index)) | ||||||||||||||||
result = join_delimiter.join(results) | ||||||||||||||||
else: | ||||||||||||||||
result = "" | ||||||||||||||||
|
||||||||||||||||
except re.error: | ||||||||||||||||
result = "" | ||||||||||||||||
|
||||||||||||||||
return result, | ||||||||||||||||
|
||||||||||||||||
NODE_CLASS_MAPPINGS = { | ||||||||||||||||
"StringConcatenate": StringConcatenate, | ||||||||||||||||
"StringSubstring": StringSubstring, | ||||||||||||||||
"StringLength": StringLength, | ||||||||||||||||
"CaseConverter": CaseConverter, | ||||||||||||||||
"StringTrim": StringTrim, | ||||||||||||||||
"StringReplace": StringReplace, | ||||||||||||||||
"StringContains": StringContains, | ||||||||||||||||
"StringCompare": StringCompare, | ||||||||||||||||
"RegexMatch": RegexMatch, | ||||||||||||||||
"RegexExtract": RegexExtract | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
NODE_DISPLAY_NAME_MAPPINGS = { | ||||||||||||||||
"StringConcatenate": "Concatenate", | ||||||||||||||||
"StringSubstring": "Substring", | ||||||||||||||||
"StringLength": "Length", | ||||||||||||||||
"CaseConverter": "Case Converter", | ||||||||||||||||
"StringTrim": "Trim", | ||||||||||||||||
"StringReplace": "Replace", | ||||||||||||||||
"StringContains": "Contains", | ||||||||||||||||
"StringCompare": "Compare", | ||||||||||||||||
"RegexMatch": "Regex Match", | ||||||||||||||||
"RegexExtract": "Regex Extract" | ||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused function. Please remove.