From ee0fbf24cf3365d2e48125c94e2fcc1e277585dc Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Tue, 12 Jan 2021 11:43:06 -0500 Subject: [PATCH] Complex enum mappings --- genbindings.py | 103 ++---------------------------- java_strings.py | 106 +++++++++++++++++++++++++++++++ typescript_strings.py | 142 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 252 insertions(+), 99 deletions(-) diff --git a/genbindings.py b/genbindings.py index e684a420..0e20b2a2 100755 --- a/genbindings.py +++ b/genbindings.py @@ -928,105 +928,12 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: def map_complex_enum(struct_name, union_enum_items): java_hu_type = struct_name.replace("LDK", "") complex_enums.add(struct_name) - with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum: - out_java_enum.write(consts.hu_struct_file_prefix) - out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n") - out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n") - out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n") - out_java_enum.write("\tprotected void finalize() throws Throwable {\n") - out_java_enum.write("\t\tsuper.finalize();\n") - out_java_enum.write("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n") - out_java_enum.write("\t}\n") - out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n") - out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n") - java_hu_subclasses = "" - - tag_field_lines = union_enum_items["field_lines"] - init_meth_jty_strs = {} - for idx, struct_line in enumerate(tag_field_lines): - if idx == 0: - assert(struct_line == "typedef enum %s_Tag {" % struct_name) - elif idx == len(tag_field_lines) - 3: - assert(struct_line.endswith("_Sentinel,")) - elif idx == len(tag_field_lines) - 2: - assert(struct_line == "} %s_Tag;" % struct_name) - elif idx == len(tag_field_lines) - 1: - assert(struct_line == "") + with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum: + (out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, union_enum_items, map_type, camel_to_snake) - out_java.write("\tpublic static class " + struct_name + " {\n") - out_java.write("\t\tprivate " + struct_name + "() {}\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n") - java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n" - out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n") - out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n") - init_meth_jty_str = "" - init_meth_params = "" - init_meth_body = "" - hu_conv_body = "" - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_ty = map_type(field.strip(' ;'), False, None, False, True) - out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") - java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n" - if field_ty.to_hu_conv is not None: - hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" - hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" - hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" - else: - hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" - init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg - if idx > 1: - init_meth_params = init_meth_params + ", " - init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name - init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " - out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ") - out_java.write(init_meth_body) - out_java.write("}\n") - out_java.write("\t\t}\n") - out_java_enum.write("\t\t}\n") - java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n" - java_hu_subclasses = java_hu_subclasses + hu_conv_body - java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n" - init_meth_jty_strs[var_name] = init_meth_jty_str - out_java.write("\t\tstatic native void init();\n") - out_java.write("\t}\n") - out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n") - out_java_enum.write(java_hu_subclasses) - out_java.write("\tstatic { " + struct_name + ".init(); }\n") - out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); - - write_c(consts.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs)) - - write_c(consts.c_fn_ty_pfx + consts.c_complex_enum_pass_ty(struct_name) + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " ptr) {\n") - write_c("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") - write_c("\tswitch(obj->tag) {\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - write_c("\t\tcase " + struct_name + "_" + var_name + ": {\n") - c_params = [] - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_map = map_type(field.strip(' ;'), False, None, False, True) - if field_map.ret_conv is not None: - write_c("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) - write_c("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) - write_c(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") - c_params.append(field_map.ret_conv_name) - else: - c_params.append("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) - write_c("\t\t\treturn " + consts.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n") - write_c("\t\t}\n") - write_c("\t\tdefault: abort();\n") - write_c("\t}\n}\n") - out_java_enum.write("}\n") + out_java_enum.write(out_java_enum_addendum) + out_java.write(out_java_addendum) + write_c(out_c_addendum) def map_trait(struct_name, field_var_lines, trait_fn_lines): with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait: diff --git a/java_strings.py b/java_strings.py index 5aefd223..0a20dcf1 100644 --- a/java_strings.py +++ b/java_strings.py @@ -443,3 +443,109 @@ import java.util.Arrays; out_c = out_c + "}\n" return ("", out_c) + + def map_complex_enum(self, struct_name, union_enum_items, map_type, camel_to_snake): + java_hu_type = struct_name.replace("LDK", "") + out_java_enum = "" + out_java = "" + out_c = "" + + out_java_enum += (self.hu_struct_file_prefix) + out_java_enum += ("public class " + java_hu_type + " extends CommonBase {\n") + out_java_enum += ("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n") + out_java_enum += ("\t@Override @SuppressWarnings(\"deprecation\")\n") + out_java_enum += ("\tprotected void finalize() throws Throwable {\n") + out_java_enum += ("\t\tsuper.finalize();\n") + out_java_enum += ("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n") + out_java_enum += ("\t}\n") + out_java_enum += ("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n") + out_java_enum += ("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n") + java_hu_subclasses = "" + + tag_field_lines = union_enum_items["field_lines"] + init_meth_jty_strs = {} + for idx, struct_line in enumerate(tag_field_lines): + if idx == 0: + assert(struct_line == "typedef enum %s_Tag {" % struct_name) + elif idx == len(tag_field_lines) - 3: + assert(struct_line.endswith("_Sentinel,")) + elif idx == len(tag_field_lines) - 2: + assert(struct_line == "} %s_Tag;" % struct_name) + elif idx == len(tag_field_lines) - 1: + assert(struct_line == "") + + out_java += ("\tpublic static class " + struct_name + " {\n") + out_java += ("\t\tprivate " + struct_name + "() {}\n") + for idx, struct_line in enumerate(tag_field_lines): + if idx != 0 and idx < len(tag_field_lines) - 3: + var_name = struct_line.strip(' ,')[len(struct_name) + 1:] + out_java += ("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n") + java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n" + out_java_enum += ("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n") + out_java_enum += ("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n") + init_meth_jty_str = "" + init_meth_params = "" + init_meth_body = "" + hu_conv_body = "" + if "LDK" + var_name in union_enum_items: + enum_var_lines = union_enum_items["LDK" + var_name] + for idx, field in enumerate(enum_var_lines): + if idx != 0 and idx < len(enum_var_lines) - 2: + field_ty = map_type(field.strip(' ;'), False, None, False, True) + out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") + java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n" + if field_ty.to_hu_conv is not None: + hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" + hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" + hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" + else: + hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" + init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg + if idx > 1: + init_meth_params = init_meth_params + ", " + init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name + init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " + out_java += ("\t\t\t" + var_name + "(" + init_meth_params + ") { ") + out_java += (init_meth_body) + out_java += ("}\n") + out_java += ("\t\t}\n") + out_java_enum += ("\t\t}\n") + java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n" + java_hu_subclasses = java_hu_subclasses + hu_conv_body + java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n" + init_meth_jty_strs[var_name] = init_meth_jty_str + out_java += ("\t\tstatic native void init();\n") + out_java += ("\t}\n") + out_java_enum += ("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n") + out_java_enum += (java_hu_subclasses) + out_java += ("\tstatic { " + struct_name + ".init(); }\n") + out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); + + out_c += (self.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs)) + + out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " ptr) {\n") + out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") + out_c += ("\tswitch(obj->tag) {\n") + for idx, struct_line in enumerate(tag_field_lines): + if idx != 0 and idx < len(tag_field_lines) - 3: + var_name = struct_line.strip(' ,')[len(struct_name) + 1:] + out_c += ("\t\tcase " + struct_name + "_" + var_name + ": {\n") + c_params = [] + if "LDK" + var_name in union_enum_items: + enum_var_lines = union_enum_items["LDK" + var_name] + for idx, field in enumerate(enum_var_lines): + if idx != 0 and idx < len(enum_var_lines) - 2: + field_map = map_type(field.strip(' ;'), False, None, False, True) + if field_map.ret_conv is not None: + out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) + out_c += ("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) + out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") + c_params.append(field_map.ret_conv_name) + else: + c_params.append("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) + out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n") + out_c += ("\t\t}\n") + out_c += ("\t\tdefault: abort();\n") + out_c += ("\t}\n}\n") + out_java_enum += ("}\n") + return (out_java, out_java_enum, out_c) diff --git a/typescript_strings.py b/typescript_strings.py index dccd8cf3..c5b32c53 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -194,9 +194,13 @@ import * as bindings from '../bindings' // TODO: figure out location out_c = "static inline " + struct_name + " " + struct_name + "_from_js(int32_t ord) {\n" out_c = out_c + "\tswitch (ord) {\n" ord_v = 0 + + out_typescript_enum_fields = "" + for var in variants: out_c = out_c + "\t\tcase %d: return %s;\n" % (ord_v, var) ord_v = ord_v + 1 + out_typescript_enum_fields += f"{var},\n\t\t\t\t" out_c = out_c + "\t}\n" out_c = out_c + "\tabort();\n" out_c = out_c + "}\n" @@ -210,7 +214,14 @@ import * as bindings from '../bindings' // TODO: figure out location out_c = out_c + "\t\tdefault: abort();\n" out_c = out_c + "\t}\n" out_c = out_c + "}\n" - return (out_c, "", "") + + out_typescript_enum = f""" + export enum {struct_name} {{ + {out_typescript_enum_fields} + }} + """ + + return (out_c, out_typescript_enum, "") def c_unitary_enum_to_native_call(self, ty_info): return (ty_info.rust_obj + "_to_js(", ")") @@ -231,3 +242,132 @@ import * as bindings from '../bindings' // TODO: figure out location def native_c_map_trait(self, struct_name, field_var_convs, field_fn_lines): return ("", "") + + def map_complex_enum(self, struct_name, union_enum_items, map_type, camel_to_snake): + java_hu_type = struct_name.replace("LDK", "") + + out_java_enum = "" + out_java = "" + out_c = "" + + out_java_enum += (self.hu_struct_file_prefix) + out_java_enum += ("export default class " + java_hu_type + " extends CommonBase {\n") + out_java_enum += ("\tprotected constructor(_dummy: object, ptr: number) { super(ptr); }\n") + out_java_enum += ("\tprotected finalize() {\n") + out_java_enum += ("\t\tsuper.finalize();\n") + out_java_enum += ("\t\tif (this.ptr != 0) { bindings." + java_hu_type + "_free(this.ptr); }\n") + out_java_enum += ("\t}\n") + out_java_enum += f"\tstatic constr_from_ptr(ptr: number): {java_hu_type} {{\n" + out_java_enum += (f"\t\tconst raw_val: bindings.{struct_name} = bindings." + struct_name + "_ref_from_ptr(ptr);\n") + java_hu_subclasses = "" + + tag_field_lines = union_enum_items["field_lines"] + init_meth_jty_strs = {} + for idx, struct_line in enumerate(tag_field_lines): + if idx == 0: + assert(struct_line == "typedef enum %s_Tag {" % struct_name) + elif idx == len(tag_field_lines) - 3: + assert(struct_line.endswith("_Sentinel,")) + elif idx == len(tag_field_lines) - 2: + assert(struct_line == "} %s_Tag;" % struct_name) + elif idx == len(tag_field_lines) - 1: + assert(struct_line == "") + + out_java += ("\tpublic static class " + struct_name + " {\n") + out_java += ("\t\tprivate " + struct_name + "() {}\n") + for idx, struct_line in enumerate(tag_field_lines): + if idx != 0 and idx < len(tag_field_lines) - 3: + var_name = struct_line.strip(' ,')[len(struct_name) + 1:] + out_java += ("\t\texport class " + var_name + " extends " + struct_name + " {\n") + java_hu_subclasses = java_hu_subclasses + "export class " + var_name + " extends " + java_hu_type + " {\n" + out_java_enum += ("\t\tif (raw_val instanceof bindings." + struct_name + "." + var_name + ") {\n") + out_java_enum += ("\t\t\treturn new " + var_name + "(this.ptr, raw_val);\n") + init_meth_jty_str = "" + init_meth_params = "" + init_meth_body = "" + hu_conv_body = "" + if "LDK" + var_name in union_enum_items: + enum_var_lines = union_enum_items["LDK" + var_name] + for idx, field in enumerate(enum_var_lines): + if idx != 0 and idx < len(enum_var_lines) - 2: + field_ty = map_type(field.strip(' ;'), False, None, False, True) + out_java += ("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") + java_hu_subclasses = java_hu_subclasses + "\tpublic " + field_ty.arg_name + f": {field_ty.java_hu_ty};\n" + if field_ty.to_hu_conv is not None: + hu_conv_body = hu_conv_body + "\t\tconst " + field_ty.arg_name + f": {field_ty.java_ty} = obj." + field_ty.arg_name + ";\n" + hu_conv_body = hu_conv_body + "\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" + hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" + else: + hu_conv_body = hu_conv_body + "\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" + init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg + if idx > 1: + init_meth_params = init_meth_params + ", " + init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name + init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " + out_java += ("\t\t\t" + var_name + "(" + init_meth_params + ") { ") + out_java += (init_meth_body) + out_java += ("}\n") + out_java += ("\t\t}\n") + out_java_enum += ("\t\t}\n") + java_hu_subclasses = java_hu_subclasses + "\tprivate constructor(ptr: number, obj: bindings." + struct_name + "." + var_name + ") {\n\t\tsuper(null, ptr);\n" + java_hu_subclasses = java_hu_subclasses + hu_conv_body + java_hu_subclasses = java_hu_subclasses + "\t}\n}\n" + init_meth_jty_strs[var_name] = init_meth_jty_str + out_java += ("\t\tstatic native void init();\n") + out_java += ("\t}\n") + out_java_enum += ("\t\tthrow new Error('oops, this should be unreachable'); // Unreachable without extending the (internal) bindings interface\n\t}\n\n") + out_java += ("\tstatic { " + struct_name + ".init(); }\n") + out_java += ("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); + + out_c += (self.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs)) + + out_c += (self.c_fn_ty_pfx + self.c_complex_enum_pass_ty(struct_name) + " " + self.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + self.c_fn_args_pfx + ", " + self.ptr_c_ty + " ptr) {\n") + out_c += ("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") + out_c += ("\tswitch(obj->tag) {\n") + for idx, struct_line in enumerate(tag_field_lines): + if idx != 0 and idx < len(tag_field_lines) - 3: + var_name = struct_line.strip(' ,')[len(struct_name) + 1:] + out_c += ("\t\tcase " + struct_name + "_" + var_name + ": {\n") + c_params = [] + if "LDK" + var_name in union_enum_items: + enum_var_lines = union_enum_items["LDK" + var_name] + for idx, field in enumerate(enum_var_lines): + if idx != 0 and idx < len(enum_var_lines) - 2: + field_map = map_type(field.strip(' ;'), False, None, False, True) + if field_map.ret_conv is not None: + out_c += ("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) + out_c += ("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) + out_c += (field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") + c_params.append(field_map.ret_conv_name) + else: + c_params.append("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) + out_c += ("\t\t\treturn " + self.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n") + out_c += ("\t\t}\n") + out_c += ("\t\tdefault: abort();\n") + out_c += ("\t}\n}\n") + out_java_enum += ("}\n") + out_java_enum += (java_hu_subclasses) + return (out_java, out_java_enum, out_c) + + + +def camel_to_snake_case(str): + res = [str[0].lower()] + for i in range(1, len(str)): + current_char = str[i] + + previous_char = None + next_char = None + if i > 0: + previous_char = str[i - 1] + if i < len(str) - 1: + next_char = str[i + 1] + + if current_char.isupper() and previous_char is not None: + if previous_char.islower() or (next_char is not None and next_char.islower()): + res.append('_') + res.append(current_char.lower()) + continue + res.append(current_char.lower()) + + return ''.join(res) -- 2.30.2