MCPcopy Create free account
hub / github.com/GRAnimated/MinecraftLCE / createNameFromString

Method createNameFromString

tools/parse_languages_loc.py:130–176  ·  view source on GitHub ↗
(self, string: str)

Source from the content-addressed store, hash-verified

128
129 # Takes a string and makes a valid variable name for it
130 def createNameFromString(self, string: str):
131 final_str = ""
132
133 words = re.findall(r"[A-Za-z0-9]+", string)
134
135 # this name is very long, lets try to shorten it a bit
136 if len(string) > 50:
137 words = words[0:8]
138
139 for i in range(len(words)):
140 words[i] = words[i].capitalize()
141
142 base_name = "".join(words) or "Unnamed" # if the string is empty
143
144 # place an underscore behind numbers at the start of a string
145 if len(base_name) > 0 and base_name[0].isnumeric():
146 base_name = "_" + base_name
147
148 if base_name not in used_names:
149 used_names[base_name] = [string]
150 return base_name
151
152 existing_list = used_names[base_name]
153
154 first_words = re.findall(r"[A-Za-z0-9]+", existing_list[0])
155 new_words = re.findall(r"[A-Za-z0-9]+", string)
156
157 diff = [w.capitalize() for w in new_words if w not in first_words]
158
159 # find the first 5 words that are different
160 if diff:
161 name = f"{base_name}{''.join(diff[:5])}"
162 else:
163 name = f"{base_name}_{len(existing_list)}"
164
165 used_names[base_name].append(string) # to count duplicates
166
167 suffix_count = 1
168 unique_name = name
169 while unique_name in used_names:
170 unique_name = f"{name}_{suffix_count}"
171 suffix_count += 1
172
173 used_names.setdefault(unique_name, [])
174 used_names[unique_name].append(string)
175
176 return unique_name
177
178 def stringReplacements(self, string: str):
179 # some things that we can shorten

Callers 1

createHeaderMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected