Code generation method and device, electronic equipment and storage medium
By setting unique identification and generating a code segment list for code segments, the problem that code generation tools cannot recognize and modify records is solved, and intelligent identification and replacement of code files is realized, manual intervention is reduced, and the efficiency and quality of code generation is improved.
Patent Information
- Application Number
- CN202411109147.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-13
- Publication Date
- 2025-01-03
AI Technical Summary
Existing code generation tools cannot accurately identify modified records in existing code files, resulting in data loss or incompleteness, requiring manual intervention from developers, increasing maintenance costs and work burden.
By setting a unique identifier for each code segment, a list of code segments is generated, searching and replacing based on the identifier, identifying and replacing the code segments to be replaced, avoiding data loss, and adding comment marks when necessary to handle the modified content.
It effectively reduces the workload of manual processing, improves the efficiency and quality of code generation, and ensures the integrity and consistency of code files.
Smart Images

Figure CN119248231A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code generation method, device, electronic device and storage medium. Background Art
[0002] With the rapid development of information technology, in the process of software development and system maintenance, it is usually necessary to regenerate code to replace the target code in the source project to achieve specific functions. In this process, code generation tools are widely used to automatically generate program code and script code. These tools can significantly improve development efficiency and reduce repetitive work. However, in special code generation scenarios, existing processing solutions have obvious limitations.
[0003] At present, there are two common methods for processing existing code files: one is to directly overwrite the file, which may cause the loss of some manually modified data, requiring developers to find the lost code; the other is to skip generating the file. Although this method retains the manually modified content, it may cause the generated code file to be incomplete, requiring developers to manually fill in the missing parts. Since the code generation tool cannot accurately determine the modification records in the existing files, nor can it intelligently merge the externally modified code with the code generated by the tool itself, this limitation not only affects the efficiency and quality of code generation, but also brings additional work burden to developers. As a result, the current solutions for processing existing code files require manual intervention by developers, which will lead to high manual maintenance costs and a lot of useless manpower consumption when a large number of files are generated or code generation operations are frequent. Summary of the invention
[0004] The purpose of the embodiments of the present application is to provide a code generation method, device, electronic device and storage medium to solve the problem in the prior art that automatic code generation cannot accurately identify code content, resulting in data loss or incompleteness. The specific technical solution is as follows:
[0005] According to a first aspect of an embodiment of the present application, a code generation method is provided, the method comprising:
[0006] Acquire an update code segment and a first identifier of the update code segment, and generate an update code segment list according to a plurality of the update code segments;
[0007] Searching for a code file to be replaced according to the updated code segment, extracting a plurality of code segments to be replaced and a second identifier of each of the code segments to be replaced from the code file to be replaced;
[0008] Taking any update code segment in the update code segment list as a target update code segment, and retrieving the second identifier based on the first target identifier of the target update code segment;
[0009] If there is a second target identifier that is consistent with the first target identifier, the code segment to be replaced corresponding to the second target identifier is used as the target replacement code segment;
[0010] The target replacement code segment is replaced with the target update code segment.
[0011] Optionally, replacing the target replacement code segment with the target update code segment includes:
[0012] Calculate an initial content digest based on the code content of the target replacement code segment;
[0013] Reading the current content summary of the target replacement code segment from the to-be-replaced code file, and comparing the current content summary with the initial content summary;
[0014] If the current content summary is consistent with the initial content summary, replacing the target replacement code segment in the to-be-replaced code file with the target update code segment;
[0015] If the current content summary is inconsistent with the initial content summary, the target replacement code segment in the to-be-replaced code file is converted into a comment code segment, and the target update code segment is added after the comment code segment.
[0016] Optionally, the path information of the code file to be replaced is extracted based on the update code segment, and the searching for the code file to be replaced according to the update code segment, and extracting a plurality of code segments to be replaced and a second identifier of each of the code segments to be replaced from the code file to be replaced, includes:
[0017] Read the code file to be replaced based on the path information to obtain a number of segment start marks and a number of segment end marks;
[0018] According to the segment start mark and the segment end mark of each group, read the code segment to be replaced and the second identifier of the code segment to be replaced from the code file to be replaced;
[0019] A list of code segments to be replaced is generated based on the plurality of code segments to be replaced.
[0020] Optionally, if there is a second target identifier that is consistent with the first target identifier, taking the to-be-replaced code segment corresponding to the second target identifier as the target replacement code segment includes:
[0021] If there is a second target identifier that is consistent with the first target identifier, the code segment to be replaced corresponding to the second target identifier is used as the target replacement code segment;
[0022] The target replacement code segment is deleted from the list of code segments to be replaced.
[0023] Optionally, after replacing the target replacement code segment with the target update code segment, the method further includes:
[0024] Read the list of code segments to be replaced to obtain the remaining code segments to be replaced;
[0025] The remaining code segments to be replaced are deleted from the code file to be replaced.
[0026] Optionally, after taking any update code segment in the update code segment list as a target update code segment and retrieving the second identifier based on the first target identifier of the target update code segment, the method further includes:
[0027] If there is no second target identifier that is consistent with the first target identifier, the target update code segment is added to the end of the code file to be replaced.
[0028] Optionally, before searching for a code file to be replaced according to the updated code segment and extracting a plurality of code segments to be replaced and a second identifier of each of the code segments to be replaced from the code file to be replaced, the method further includes:
[0029] Extracting the path information of the code file to be replaced based on the update code segment;
[0030] The code file to be replaced is retrieved based on the path information, and if the code file to be replaced does not exist, the updated code segment is output according to the updated code segment list.
[0031] According to a second aspect of an embodiment of the present application, a code generation device is provided, the device comprising:
[0032] An update list generating module, used for acquiring an update code segment and a first identifier of the update code segment, and generating an update code segment list according to a plurality of the update code segments;
[0033] A code segment to be replaced extraction module, used for searching a code file to be replaced according to the updated code segment, and extracting a plurality of code segments to be replaced and a second identifier of each code segment to be replaced from the code file to be replaced;
[0034] a target identifier retrieval module, configured to take any update code segment in the update code segment list as a target update code segment, and retrieve the second identifier based on the first target identifier of the target update code segment;
[0035] a target replacement code segment confirmation module, configured to use the to-be-replaced code segment corresponding to the second target identifier as the target replacement code segment if there is a second target identifier that is consistent with the first target identifier;
[0036] The code segment replacement execution module is used to replace the target replacement code segment with the target update code segment.
[0037] According to a third aspect of an embodiment of the present application, there is provided an electronic device, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0038] The memory is used to store computer programs;
[0039] The processor is used to implement the code generation method as described in any one of the first aspects when executing the program stored in the memory.
[0040] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, on which instructions are stored. When the instructions are executed by one or more processors, the processors execute the code generation method as described in any one of the first aspects.
[0041] Compared with the prior art, the technical solution provided by the embodiment of the present application has the following beneficial effects:
[0042] The code generation method disclosed in the present application generates an update code segment list according to a plurality of update code segments by acquiring an update code segment and a first identifier of the update code segment; searches for a code file to be replaced according to the update code segment, extracts a plurality of code segments to be replaced and a second identifier of each code segment to be replaced from the code file to be replaced; uses any update code segment in the update code segment list as a target update code segment, and retrieves the second identifier based on the first target identifier of the target update code segment; if there is a second target identifier that is consistent with the first target identifier, uses the code segment to be replaced corresponding to the second target identifier as the target replacement code segment; and replaces the target replacement code segment with the target update code segment. The present application sets a unique identifier for each code segment and generates a corresponding code segment list, selects a target update code segment from the code segment list, and retrieves all the code segments to be replaced in the file to be replaced based on the first target identifier of the target update code segment, and replaces the code segments to be replaced in the code file to be replaced according to the search results. Furthermore, summary content can be generated for each code segment, and the code segment containing the modified content can be identified based on the comparison results of the summary content. Through intelligent identification of code segments, the workload of manual processing is effectively reduced, and the efficiency and quality of code generation are improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0044] Figure 1 It is a flowchart of a code generation method provided by an embodiment of the present application;
[0045] Figure 2 It is a structural block diagram of a code generation device provided in an embodiment of the present application;
[0046] Figure 3 It is a block diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0047] In order to make the above-mentioned purposes, features and advantages of the present application more obvious and understandable, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0048] In order to enable those skilled in the art to better understand the technical solutions in the embodiments of the present application, some technical features involved in the embodiments of the present application are explained and illustrated below:
[0049] Code generation is an important part of the software development process. It refers to the process of automatically creating or modifying source code through software tools or algorithms. This process can significantly improve development efficiency, reduce repetitive work, and help developers follow consistent coding standards and best practices.
[0050] Code Updating refers to applying generated code to an existing code base to update or replace existing code. This process may involve merging, replacing, or inserting new code segments.
[0051] Code merging refers to merging two or more code versions into a single version. During the code update process, code merging may be required to ensure that the generated code and the existing code can be correctly combined.
[0052] Code generation tools are software tools that can automatically generate program code based on predefined templates, rules or models. These tools are widely used in the software development process to improve development efficiency, reduce repetitive work, and ensure code consistency and quality.
[0053] As an example, during software development and system maintenance, it is usually necessary to regenerate code to replace the target code in the source project. Currently, the existing target code is usually processed by overwriting files or skipping generation. The file overwriting method may cause the loss of some manually modified data in the target code, and the skipping generation method may cause the regenerated code file to be incomplete, thereby failing to implement the corresponding functions. Both methods require additional manual intervention. When a large number of files are generated or there are frequent code generation operations, high manual maintenance costs will be incurred, and the efficiency of code generation will be affected.
[0054] In this regard, in an embodiment of the present application, a unique identifier is set for each code segment and a corresponding code segment list is generated, a target update code segment is selected based on the code segment list, and the code segment in the target file is retrieved based on the unique identifier of the target update code segment, and the code segment in the target file is replaced according to the retrieval result. Furthermore, summary content can be generated for each code segment, and the code segment containing the modified content can be identified based on the comparison result of the summary content. Through intelligent identification of code segments, the workload of manual processing is effectively reduced, and the efficiency and quality of code generation are improved.
[0055] Reference Figure 1 , shows a flowchart of a code generation method provided by an embodiment of the present application, which may specifically include the following steps:
[0056] S101, obtaining an update code segment and a first identifier of the update code segment, and generating an update code segment list according to a plurality of update code segments.
[0057] In an embodiment of the present application, the regenerated code content is segmented based on the code content characteristics. For example, for a program code file, a method can be used as a code segment, for a table maintenance script, a table-related script can be used as a code segment, for a functional programming file, a function can be used as a code segment, and so on. The regenerated update code content is segmented based on the code content characteristics to obtain a number of update code segments, and the update code segments represent the regenerated code segments.
[0058] Furthermore, in an embodiment of the present application, a unique identifier is set for each code segment. For easy distinction, the unique identifier of the updated code segment is recorded as the first identifier, and the unique identifier of each code segment is a mark used to identify the uniqueness of the code segment. When reading the updated code segment, the first identifier of the updated code segment can be read at the same time, and the acquired updated code segment and its first identifier are stored in a list to generate an updated code segment list, thereby managing a number of updated code segments. Preferably, in order to avoid the unique identifier affecting the compilation or execution of the code file, the unique identifier generated for each code segment can be set in the code comment.
[0059] Among them, the unique identifier can be generated in a variety of ways. For example, the unique identifier can be generated based on the name of the code segment. For example, for a method code segment, the unique identifier of the method code segment can be calculated based on the expression substr(md5(method_name),0,8). Specifically, the field method_name is hashed using the MD5 hash algorithm to obtain a first hash value, wherein MD5 is a widely used hash function that can convert data of any length into a 128-bit hash value. The hash value is usually represented as 32 hexadecimal characters. The field method_name usually refers to the name of the method or function. The substr function is then used to extract the first 8 characters from the first hash value as the unique identifier of the method code segment. For a database table script, the unique identifier of the script code segment can be calculated based on the expression substr(md5(table_name),0,8). Specifically, the field table_name is hashed using the MD5 hash algorithm to obtain a second hash value, wherein the field table_name usually refers to the name of the table in the database, and the substr function is then used to extract the first 8 characters from the hash value as the unique identifier of the script code segment. Automatically generate a unique identifier for each code segment based on an expression, effectively improving processing efficiency and consistency. At the same time, the identifier generated by the MD5 hash algorithm and string extraction has a high degree of uniqueness. Even if the method name or table name of the code segment changes slightly, its hash value will be significantly different, ensuring the uniqueness of the identifier.
[0060] S102, searching for a code file to be replaced according to the updated code segment, and extracting a plurality of code segments to be replaced and a second identifier of each code segment to be replaced from the code file to be replaced.
[0061] During the generation process of the update code segment, the generation path or storage path of the code segment can be obtained by reading configuration files, environment variables or directly specifying the path in the code. For example, if the update code segment is generated based on a code template, the storage path of the update code segment can be obtained through the input parameters and output format of the code template. If the update code segment is generated by a code generation tool or script, the storage path of the update code segment can be obtained by reading the configuration items in the code generation tool or script.
[0062] Specifically, the updated code segment, as a regenerated code, needs to replace the original code file, that is, according to the storage path of the updated code segment, the one pointed to is the original code file that needs to be replaced. Therefore, the code file to be replaced can be found based on the path information of the updated code segment. Based on the uniformity of the data, the code content in the code file to be replaced is also segmented, and a unique identifier is generated for each code segment in the code file to be replaced. Then, by reading the code file to be replaced, several code segments to be replaced and the second identifier of each code segment to be replaced can be extracted. The acquired code segments to be replaced and their second identifiers are stored in a list to generate a list of code segments to be replaced, thereby managing several code segments to be replaced.
[0063] Optionally, in order to improve the reading accuracy of the code segment in the code file, a segment start mark and a segment end mark can be set for each code segment. In the automatic reading process of the code file, the code segment between the segment start mark and the segment end mark can be accurately read by matching the segment start mark and the segment end mark. For example, by reading the code file to be replaced, several segment start marks and several segment end marks can be obtained, and a segment start mark and a segment end mark can be recorded as a group. According to each group of segment start marks and segment end marks, several code segments to be replaced in the code file to be replaced can be read. At the same time, in order to prevent the segment start mark and the segment end mark from affecting the compilation and normal execution of the code file, the segment start mark and the segment end mark can be set in the code comment.
[0064] In one example, path information can be extracted based on the update code segment. The path information here is both the path information of the update code segment and the path information of the code file to be replaced. The code file to be replaced is retrieved based on the path information, and it is determined whether there is a generated code file to be replaced at the file location pointed to by the path information. When the code file to be replaced does not exist, it indicates that there is no generated code file at the file location. At this time, there is no need to perform code replacement, and a code file can be created, and several updated code segments can be output in sequence according to the update code segment list, effectively reducing unnecessary retrieval and replacement operations and improving code generation efficiency.
[0065] S103: taking any update code segment in the update code segment list as a target update code segment, and searching for the second identifier based on the first target identifier of the target update code segment.
[0066] In an embodiment of the present application, several update code segments are aggregated and managed through an update code segment list, and an update code segment is selected from the update code segment list as a target update code segment. The selection can be random or based on specific rules or conditions. Preferably, to ensure the integrity of the code replacement, the code segment to be replaced can be traversed to complete the retrieval process of the code segment to be replaced. Exemplarily, the update code segment list can be traversed, and each update code segment can be sequentially taken as a target update code segment to retrieve all the code segments to be replaced. Any update code in the update code segment list can also be taken as a target update code segment to retrieve all the codes to be replaced, so as to realize the traversal process of all update code segments.
[0067] After selecting the target update code segment, the first target identifier of the target update code segment is obtained, and all the code segments to be replaced are matched based on the first target identifier. Specifically, all the second identifiers can be matched based on the first target identifier. Since the first target identifier and the second identifier can uniquely mark a code segment, when the first target identifier and the second identifier match each other, it can be said that the code segment to be replaced corresponds to the target update code segment.
[0068] In one example, if there is no second target identifier that is consistent with the first target identifier, it means that there is no code segment corresponding to the target update code segment in all the code segments to be replaced, that is, in the original target file, there is no generated code segment that corresponds to the target update code segment. At this time, there is no need to continue the code replacement process, and the target update code segment can be added to the end of the file to be replaced.
[0069] S104: If there is a second target identifier that is consistent with the first target identifier, the to-be-replaced code segment corresponding to the second target identifier is used as a target replacement code segment.
[0070] Based on the first target identifier of the target update code segment, the second identifiers of all the codes to be replaced are retrieved. If there is a second target identifier that is consistent with the first target identifier, it means that there is a code segment corresponding to the target update code segment in the file to be replaced, that is, in the original target file, there is a generated code segment that has a corresponding relationship with the target update code segment. The code segment to be replaced corresponding to the second target identifier is used as the target replacement code segment, and code replacement is performed on the target replacement code segment.
[0071] Specifically, all code segments in the code file to be replaced can be managed through the code segment list to be replaced. When the target replacement code segment is determined, the target replacement code segment is deleted from the code segment list to be replaced. At this time, the code segment list to be replaced reflects the real-time changes of the code segment content in the code file to be replaced.
[0072] S105, replacing the target replacement code segment with the target update code segment.
[0073] Optionally, to avoid data loss caused by direct code replacement, before replacing the target replacement code segment with the target update code segment, it is also necessary to determine whether the target replacement code contains manually modified content. In order to quickly read the code content of each code segment, a content summary can be set for each code segment to reflect the code content in the current code segment. For example, the content summary can be generated using the expression substr(md5(content),0,8). Specifically, the MD5 hash algorithm is used to hash the field content to obtain a third hash value, and the first 8 bits of the third hash value are taken as the content summary of the code segment, wherein the field content can represent the code content of the code segment. The code content contained in the code segment can be quickly obtained through the content summary. When the code segment is modified and contains manually modified content, the content summary will also change accordingly. Therefore, by comparing the content summaries, it can be determined whether the current code segment contains manually modified content.
[0074] Similarly, in order to avoid the content summary affecting the compilation and execution of the code file, the content summary can be set in the code comment. Preferably, a 16-bit string is used to store the unique identifier and content summary of the current code segment in the code comment, wherein the first 8 bits of the string are the unique identifier, the last 8 bits of the string are the content summary, and the segment start mark and the segment end mark are located before the string. For example, in the code comment "**gen**2fio3jaowfdjf0m3**", "**gen**" is represented as the segment start mark, and after the segment start mark, "2fio3jaowfdjf0m3" is represented as a 16-bit string, wherein the first 8 characters "2fio3jao" are represented as the unique identifier, and the last 8 characters "wfdjf0m3" are represented as the content summary.
[0075] Optionally, the determination of whether the target replacement code segment contains modified content may be implemented by the following steps:
[0076] Calculate an initial content summary based on the code content of the target replacement code segment;
[0077] Read the current content summary of the target replacement code segment from the code file to be replaced, and compare the current content summary with the initial content summary;
[0078] If the current content summary is consistent with the initial content summary, the target replacement code segment in the code file to be replaced is replaced with the target update code segment;
[0079] If the current content summary is inconsistent with the initial content summary, the target replacement code segment in the code file to be replaced is converted into a comment code segment, and the target update code segment is added after the comment code segment.
[0080] Specifically, after determining the target replacement code segment, the initial content summary of the target replacement code segment can be calculated based on the expression substr(md5(content),0,8), and the last 8 characters can be read from the string of the code comment to obtain the current content summary of the target replacement code segment. By comparing the two, it can be determined whether the code to be replaced contains modified content.
[0081] If the current content summary is consistent with the initial content summary, it indicates that the content summary of the target replacement code segment has not changed, that is, the code content of the target replacement code segment has not changed. At this time, the target replacement code segment in the code file to be replaced can be replaced with the target update code segment to replace the code content in the code file; if the current content summary is inconsistent with the initial content summary, it indicates that the content summary of the target replacement code segment has changed, that is, the code content of the target replacement code segment has been modified and contains manually modified content. To avoid data loss caused by code replacement, a comment mark can be used to convert the target replacement code segment in the replacement code file into a comment code segment, and the target update code segment is added after the comment code segment. Preferably, after converting the target replacement code segment into a comment code segment, a merge mark is added after the code comment of the target replacement code segment. Exemplarily, a "**need_merge**" mark can be added after the code comment of the target replacement code segment to indicate that there is modified content in the code segment and manual merging is required. Usually, the scenario of manual code modification is less likely to occur, and adding a merge mark here can serve as a supplement to unexpected situations.
[0082] In one possible implementation, after code replacement is performed on the target replacement code segment, the list of codes to be replaced can be read to obtain the remaining code segments to be replaced in the list, and the remaining code segments to be replaced can be deleted from the code file to be replaced. Exemplarily, the regenerated update code segments do not correspond one-to-one to the code segments to be replaced in the code file to be replaced. New update code segments may appear without corresponding code segments to be replaced, and code segments to be replaced may appear in the code generation process without corresponding update code segments. After the code replacement is completed, there will still be code segments to be replaced that do not have corresponding update code segments in the code file to be replaced. Since there are no corresponding update code segments, the code replacement process does not involve operations on these code segments to be replaced, such as deletion, annotation, replacement, etc. At this time, according to the list of code segments to be replaced, redundant code segments, that is, the remaining code segments to be replaced, are obtained, and the remaining code segments to be replaced are deleted from the code file to be replaced to complete the entire code generation process.
[0083] The present application sets a unique identifier for each code segment and generates a corresponding code segment list, selects a target update code segment from the code segment list, and retrieves all the code segments to be replaced in the file to be replaced based on the first target identifier of the target update code segment, and replaces the code segments to be replaced in the code file to be replaced according to the search results. Furthermore, summary content can be generated for each code segment, and the code segment containing the modified content can be identified based on the comparison results of the summary content. Through intelligent identification of code segments, the workload of manual processing is effectively reduced, and the efficiency and quality of code generation are improved.
[0084] It should be noted that, for the method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present application are not limited by the described order of actions, because according to the embodiments of the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present application.
[0085] Reference Figure 2 , shows a structural block diagram of a code generation device provided in an embodiment of the present application, which may specifically include the following modules:
[0086] An update list generating module 201 is used to obtain an update code segment and a first identifier of the update code segment, and generate an update code segment list according to a plurality of the update code segments;
[0087] A code segment to be replaced extraction module 202, configured to search for a code file to be replaced according to the updated code segment, and extract a plurality of code segments to be replaced and a second identifier of each code segment to be replaced from the code file to be replaced;
[0088] A target identifier retrieval module 203, configured to take any update code segment in the update code segment list as a target update code segment, and retrieve the second identifier based on the first target identifier of the target update code segment;
[0089] a target replacement code segment confirmation module 204, configured to use the to-be-replaced code segment corresponding to the second target identifier as the target replacement code segment if there is a second target identifier that is consistent with the first target identifier;
[0090] The code segment replacement execution module 205 is used to replace the target replacement code segment with the target update code segment.
[0091] Optionally, the code segment replacement execution module 205 further includes:
[0092] An initial content summary calculation module, used to calculate an initial content summary based on the code content of the target replacement code segment;
[0093] A content summary comparison module, configured to read the current content summary of the target replacement code segment from the to-be-replaced code file, and compare the current content summary with the initial content summary;
[0094] a consistency comparison execution module, configured to replace the target replacement code segment in the to-be-replaced code file with the target update code segment if the current content summary is consistent with the initial content summary;
[0095] The inconsistency comparison execution module is used to convert the target replacement code segment in the code file to be replaced into a comment code segment if the current content summary is inconsistent with the initial content summary, and add the target update code segment after the comment code segment.
[0096] Optionally, the path information of the code file to be replaced is extracted based on the updated code segment, and the code segment to be replaced extraction module 202 further includes:
[0097] A segment mark acquisition module, used for reading the code file to be replaced based on the path information, and obtaining a plurality of segment start marks and a plurality of segment end marks;
[0098] A code segment to be replaced extraction submodule, used for reading the code segment to be replaced and the second identifier of the code segment to be replaced from the code file to be replaced according to the segment start mark and the segment end mark of each group;
[0099] The to-be-replaced list generating module is used to generate a to-be-replaced code segment list based on a plurality of the to-be-replaced code segments.
[0100] Optionally, the target replacement code segment confirmation module 204 further includes:
[0101] a target replacement code segment confirmation submodule, configured to, if there is a second target identifier that is consistent with the first target identifier, use the to-be-replaced code segment corresponding to the second target identifier as the target replacement code segment;
[0102] The to-be-replaced list processing module is used to delete the target replacement code segment from the to-be-replaced code segment list.
[0103] Optionally, the code generating device further includes:
[0104] A module for reading the remaining code segments in the list to be replaced, which reads the list of code segments to be replaced and obtains the remaining code segments to be replaced;
[0105] The remaining segment processing module is used to delete the remaining code segments to be replaced from the code file to be replaced.
[0106] Optionally, the code generating device further includes:
[0107] The target update code segment adding module is used to add the target update code segment to the end of the code file to be replaced if there is no second target identifier consistent with the first target identifier.
[0108] Optionally, the code generating device further includes:
[0109] A path information extraction module, used to extract the path information of the code file to be replaced based on the update code segment;
[0110] The update code segment output module is used to retrieve the code file to be replaced based on the path information, and if the code file to be replaced does not exist, output the update code segment according to the update code segment list.
[0111] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0112] refer to Figure 3 , an embodiment of the present application also provides an electronic device, including a memory 303, a processor 301, a communication interface 302, and a program stored on the memory and executable on the processor; wherein the processor is configured to read the program on the memory and execute the code generation method disclosed in any of the above embodiments.
[0113] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0114] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0115] The embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, each process of the above-mentioned code generation method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0116] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0117] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0118] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in the present application can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0119] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A code generation method, characterized in that: The method comprises: Acquire an update code segment and a first identifier of the update code segment, and generate an update code segment list according to a plurality of the update code segments; Searching for a code file to be replaced according to the updated code segment, extracting a plurality of code segments to be replaced and a second identifier of each of the code segments to be replaced from the code file to be replaced; Taking any update code segment in the update code segment list as a target update code segment, and retrieving the second identifier based on the first target identifier of the target update code segment; If there is a second target identifier that is consistent with the first target identifier, the code segment to be replaced corresponding to the second target identifier is used as the target replacement code segment; The target replacement code segment is replaced with the target update code segment.
2. The method according to claim 1, characterized in that The step of replacing the target replacement code segment with the target update code segment comprises: Calculate an initial content digest based on the code content of the target replacement code segment; Reading the current content summary of the target replacement code segment from the to-be-replaced code file, and comparing the current content summary with the initial content summary; If the current content summary is consistent with the initial content summary, replacing the target replacement code segment in the to-be-replaced code file with the target update code segment; If the current content summary is inconsistent with the initial content summary, the target replacement code segment in the to-be-replaced code file is converted into a comment code segment, and the target update code segment is added after the comment code segment.
3. The method according to claim 1, characterized in that The path information of the code file to be replaced is extracted based on the updated code segment, searching for the code file to be replaced according to the updated code segment, and extracting a plurality of code segments to be replaced and a second identifier of each of the code segments to be replaced from the code file to be replaced include: Read the code file to be replaced based on the path information to obtain a number of segment start marks and a number of segment end marks; According to the segment start mark and the segment end mark of each group, read the code segment to be replaced and the second identifier of the code segment to be replaced from the code file to be replaced; A list of code segments to be replaced is generated based on the plurality of code segments to be replaced.
4. The method according to claim 3, characterized in that If there is a second target identifier that is consistent with the first target identifier, taking the to-be-replaced code segment corresponding to the second target identifier as the target replacement code segment, comprises: If there is a second target identifier that is consistent with the first target identifier, the code segment to be replaced corresponding to the second target identifier is used as the target replacement code segment; The target replacement code segment is deleted from the list of code segments to be replaced.
5. The method according to claim 3, characterized in that: After replacing the target replacement code segment with the target update code segment, the method further includes: Read the list of code segments to be replaced to obtain the remaining code segments to be replaced; The remaining code segments to be replaced are deleted from the code file to be replaced.
6. The method according to claim 1, characterized in that After taking any update code segment in the update code segment list as a target update code segment and retrieving the second identifier based on the first target identifier of the target update code segment, the method further includes: If there is no second target identifier that is consistent with the first target identifier, the target update code segment is added to the end of the code file to be replaced.
7. The method according to claim 1, characterized in that Before searching for a code file to be replaced according to the updated code segment and extracting a plurality of code segments to be replaced and a second identifier of each of the code segments to be replaced from the code file to be replaced, the method further includes: Extracting the path information of the code file to be replaced based on the update code segment; The code file to be replaced is retrieved based on the path information, and if the code file to be replaced does not exist, the updated code segment is output according to the updated code segment list.
8. A code generating device, characterized in that: The device comprises: An update list generating module, used for acquiring an update code segment and a first identifier of the update code segment, and generating an update code segment list according to a plurality of the update code segments; A code segment to be replaced extraction module, used for searching a code file to be replaced according to the updated code segment, and extracting a plurality of code segments to be replaced and a second identifier of each code segment to be replaced from the code file to be replaced; a target identifier retrieval module, configured to take any update code segment in the update code segment list as a target update code segment, and retrieve the second identifier based on the first target identifier of the target update code segment; a target replacement code segment confirmation module, configured to use the to-be-replaced code segment corresponding to the second target identifier as the target replacement code segment if there is a second target identifier that is consistent with the first target identifier; The code segment replacement execution module is used to replace the target replacement code segment with the target update code segment.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; The memory is used to store computer programs; The processor is used to implement the code generation method according to any one of claims 1 to 7 when executing the program stored in the memory.
10. A computer-readable storage medium having instructions stored thereon, which, when executed by one or more processors, causes the processors to execute the code generation method according to any one of claims 1 to 7.
Citation Information
Cited By
Customized content merging method and system in cross-language code conversion based on marks
CN122044567A
Code updating method, device, equipment and product
CN122064364A