-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,322 @@ | ||
| import re | ||
|
|
||
| from comfy.comfy_types.node_typing import IO | ||
|
|
||
| class StringConcatenate(): | ||
| @classmethod | ||
| def INPUT_TYPES(s): | ||
| return { | ||
| "required": { | ||
| "string_a": (IO.STRING, {"multiline": True}), | ||
| "string_b": (IO.STRING, {"multiline": True}) | ||
| } | ||
| } | ||
|
|
||
| RETURN_TYPES = (IO.STRING,) | ||
| FUNCTION = "execute" | ||
| CATEGORY = "utils/string" | ||
|
|
||
| def execute(self, string_a, string_b, **kwargs): | ||
| return string_a + 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": (IO.COMBO, {"options": ["UPPERCASE", "lowercase", "Capitalize", "Title Case"]}) | ||
| } | ||
| } | ||
|
|
||
| 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": (IO.COMBO, {"options": ["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": (IO.COMBO, {"options": ["Starts With", "Ends With", "Equal"]}), | ||
| "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 == "Starts With": | ||
| return a.startswith(b), | ||
| elif mode == "Ends With": | ||
| 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": (IO.COMBO, {"options": ["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" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.