Method, device and equipment for extracting constant value of source code text and medium

By filtering comment lines and blank lines in the source code text to detect and extract target constant values, the problem of code recompilation caused by frequent modification of constant values ​​is solved, thus improving the convenience of code development.

CN120950122APending Publication Date: 2025-11-14SHANGHAI SMARTLOGIC TECHNOLOGY LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511060804.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-30
Publication Date
2025-11-14

AI Technical Summary

Technical Problem

In existing technologies, fixed constant values ​​in the source code need to be frequently modified to adapt to different scenarios, which requires the code to be recompiled and causes inconvenience to users.

Method used

By obtaining the header file from the original code text, filtering out comment lines and blank lines, traversing each line of code to detect the target constant name, and extracting the target constant value based on the detection results, the direct compilation to obtain the constant value is avoided.

Benefits of technology

This effectively avoids the code recompilation problem caused by frequent modification of constant values, and improves the convenience of the code development process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120950122A_ABST
    Figure CN120950122A_ABST
Patent Text Reader

Abstract

The invention discloses a source code text constant value extraction method and device, equipment and a medium, and the method comprises the steps: obtaining a header file in an original code text, and filtering annotation lines and null lines of a source code in the header file; traversing each row of codes in the filtered source code, and detecting whether a target constant name exists in each row of codes or not; and extracting a target constant value corresponding to the target constant name according to a detection result. According to the technical scheme, the problem that the code needs to be recompiled due to frequent modification of the constant value can be effectively avoided, and the convenience of the code development process is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a method, apparatus, device, and medium for extracting constant values ​​from source code text. Background Technology

[0002] In code projects, there exist fixed constants, manifested in various formats such as fixed values, arrays of fixed values, and macro definitions. These fixed constants require different values ​​depending on the system architecture and the scenario. Since these fixed constants are usually defined in header files, it becomes necessary to modify and adapt their values ​​for different scenarios, or to write these different values ​​in different header files. However, whenever a system architecture or scenario changes, the fixed constant values ​​need to be frequently modified, causing the code to be recompiled each time, which is inconvenient for users.

[0003] Existing source code analysis methods are mainly used for code cloning and reuse, but they cannot extract useful information from the source code to avoid directly recompiling the code. Summary of the Invention

[0004] This invention provides a method, apparatus, device, and storage medium for extracting constant values ​​from source code text, which can effectively avoid the problem of code recompilation caused by frequent modification of constant values, and improve the convenience of the code development process.

[0005] According to one aspect of the present invention, a method for extracting constant values ​​from source code text is provided, the method comprising:

[0006] Obtain the header file from the original code text, and filter the comment lines and blank lines in the source code of the header file;

[0007] In the filtered source code, each line of code is traversed to check if the target constant name exists in each line of code;

[0008] Based on the detection results, the target constant values ​​corresponding to the target constant names are extracted.

[0009] Optionally, the comment lines in the source code of the header file can be filtered, including:

[0010] The source code is traversed line by line to check if there are any preset comment characters in each line of code;

[0011] Based on the detection results, a comment identifier is established for each line of code. The comment text is then determined based on the comment identifier and filtered.

[0012] Optionally, each line of code in the filtered source code is traversed to check if the target constant name exists in each line of code. Based on the detection results, the target constant value corresponding to the target constant name is extracted, including:

[0013] Extract one line of code from the filtered source code and use it as the current line of code.

[0014] Check if the target constant name exists in the current line of code, and establish the target constant flag corresponding to the current line of code based on the detection result;

[0015] If the target constant flag is true, then the target constant value corresponding to the target constant name in the current line of code is extracted;

[0016] If the target constant flag is false, then return to the operation of sequentially retrieving one line of code from the filtered source code as the current line of code, until the processing of all source code is completed.

[0017] Optionally, extract the target constant value corresponding to the target constant name in the current line of code, including:

[0018] Determine whether square brackets exist after the target constant name in the current line of code;

[0019] If so, then determine that the target constant value corresponding to the target constant name is an array, and extract the array;

[0020] If not, then the target constant value corresponding to the target constant name is determined to be a single value, and the single value is extracted.

[0021] Optionally, the array may be extracted, including:

[0022] Replace the preset characters in the current line of code with spaces; the preset characters include at least one of the following: equal sign, comma, semicolon, and parentheses;

[0023] Divide the current line of code into a string array based on spaces;

[0024] Based on the number of strings in the string array and the array end marker corresponding to the current line of code, each string is converted into a double number for storage.

[0025] Optionally, if the target constant value corresponding to the target constant name is a two-dimensional array, then each of the strings is converted to a double number for storage, including:

[0026] Extract the target value within the second square bracket in the two-dimensional array;

[0027] Convert each of the strings into a double number and store it in the target one-dimensional array;

[0028] If the length of the target one-dimensional array is equal to the target value, then the target one-dimensional array is stored in the target two-dimensional array;

[0029] Return to the operation of converting each of the strings into double numbers and storing them in the target one-dimensional array, until all the values ​​of all rows in the two-dimensional array have been stored.

[0030] Optionally, if the target constant value corresponding to the target constant name is a three-dimensional array, then each of the strings is converted to a double number for storage, including:

[0031] Extract the target value within the second and third square brackets of the three-dimensional array;

[0032] Convert each string into a double number and store it in the target one-dimensional array;

[0033] If the length of the target one-dimensional array is equal to the target value within the third square bracket, then the target one-dimensional array is stored in the target two-dimensional array;

[0034] If the length of the target two-dimensional array is equal to the target value within the second square brackets, then the target two-dimensional array is stored in the target three-dimensional array;

[0035] Return to the operation of converting each of the strings into double numbers and storing them in the target one-dimensional array, until all the values ​​of all rows in the three-dimensional array have been stored.

[0036] According to another aspect of the present invention, a constant value extraction device for source code text is provided, the device comprising:

[0037] The code filtering module is used to obtain the header file in the original code text and filter the comment lines and blank lines in the source code of the header file;

[0038] The name detection module is used to traverse each line of code in the filtered source code and detect whether the target constant name exists in each line of code;

[0039] The constant value extraction module is used to extract the target constant value corresponding to the target constant name based on the detection results.

[0040] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising:

[0041] At least one processor; and

[0042] A memory communicatively connected to the at least one processor; wherein,

[0043] The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the constant value extraction method of source code text according to any embodiment of the present invention.

[0044] According to another aspect of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium storing computer instructions for causing a processor to execute and implement the constant value extraction method of source code text according to any embodiment of the present invention.

[0045] According to another aspect of the present invention, a computer program product is provided, the computer program product comprising a computer program that, when executed by a processor, implements the constant value extraction method for source code text as described in any embodiment of the present invention.

[0046] The technical solution provided by this invention provides a method for analyzing and extracting target constant values ​​from the original code header file, rather than directly obtaining constant values ​​through compilation. This method involves obtaining the header file from the original code text, filtering the comment lines and blank lines in the source code of the header file, traversing each line of code in the filtered source code, detecting whether a target constant name exists in each line of code, and extracting the target constant value corresponding to the target constant name based on the detection result. No matter how the header file changes, it will not affect the source code, effectively avoiding the problem of needing to recompile the code due to frequent modification of constant values, and improving the convenience of the code development process.

[0047] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description

[0048] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0049] Figure 1 This is a flowchart of a method for extracting constant values ​​from source code text according to an embodiment of the present invention;

[0050] Figure 2 This is a flowchart of another method for extracting constant values ​​from source code text according to an embodiment of the present invention;

[0051] Figure 3a This is a flowchart of a method for initializing a header file according to an embodiment of the present invention;

[0052] Figure 3b This is a flowchart of a method for filtering comment lines in a header file according to an embodiment of the present invention;

[0053] Figure 3c This is a flowchart of a method for extracting a target constant value when it is a single value, according to an embodiment of the present invention.

[0054] Figure 3d This is a flowchart of a method for extracting a target constant value when it is a one-dimensional array, according to an embodiment of the present invention.

[0055] Figure 3e This is a flowchart of a method for extracting target constant values ​​when they are two-dimensional arrays, according to an embodiment of the present invention.

[0056] Figure 3f This is a flowchart of a method for extracting target constant values ​​when they are three-dimensional arrays, according to an embodiment of the present invention.

[0057] Figure 4 This is a schematic diagram of a constant value extraction device for source code text according to an embodiment of the present invention;

[0058] Figure 5 This is a schematic diagram of the structure of an electronic device that implements the constant value extraction method of source code text in this embodiment of the invention. Detailed Implementation

[0059] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0060] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0061] Figure 1 This is a flowchart illustrating a method for extracting constant values ​​from source code text according to an embodiment of the present invention. This embodiment is applicable to extracting constant values ​​from source code within a code project. The method can be executed by a source code text constant value extraction device, which can be implemented in hardware and / or software and configured in an electronic device. Figure 1 As shown, the method includes:

[0062] Step 110: Obtain the header file from the original code text, and filter the comment lines and blank lines in the source code of the header file.

[0063] In this embodiment, after obtaining the header file source code, the code content can be read line by line and comment lines and blank lines can be filtered. For example, some or all characters of the current line that meet the filtering conditions can be set to space characters.

[0064] In this embodiment, optionally, filtering the comment lines of the source code in the header file includes: traversing each line of code in the source code and detecting whether there are preset comment characters in each line of code; establishing comment flags (flag_find_start and flag_find_end) for each line of code based on the detection results; determining the comment text based on the comment flags; and filtering the comment text.

[0065] In one specific implementation, the header file content can be read based on the input header file path. Characters following " / / " in each line of code can be set to spaces. In C, " / / " indicates that the following characters are comment text. Then, characters appearing between " / *" and "* / " on the same line can be filtered out.

[0066] In C, the characters " / *" and "* / " usually appear together, and the characters between them are comments. However, there are also scenarios where " / *" and "* / " span across lines. This happens when only " / *" is found on the current line of code, and "* / " is not found. In this case, a flag `flag_find_start` needs to be set to `true` to record that " / *" was found on the current line of code, and a flag `flag_find_end` needs to be set to `false` to record that "* / " was not found on the current line of code. Then, the next line of code is read. If "* / " is not found, both `flag_find_start` and `flag_find_end` are set to `false`. This determines that the entire line is between " / *" and "* / ", meaning the entire line will be filtered.

[0067] If the character "* / " is found in a line of code, the flag_find_end needs to be set to true, and all characters before the "* / " character in that line need to be filtered.

[0068] A specific filtering method could be to set all characters containing " / *" and "* / " and everything between them to spaces. Furthermore, due to the randomness or irregularity of code comments, multiple " / *" or multiple "* / " characters might appear on the same line. If " / *" and "* / " appear on the same line, the characters between the first occurrence of " / *" and "* / " on that line need to be filtered before searching the line again for further filtering, until all " / *" and "* / " characters on that line are completely filtered out. After the above processing, if only a single " / *" remains in the line, it indicates that "* / " might appear in subsequent lines, requiring the logic to filter " / *" and "* / " across lines.

[0069] The advantage of this setting is that by filtering comment lines and blank lines, invalid code statements can be avoided from affecting the subsequent constant value extraction results, thereby improving the accuracy and reliability of the constant value extraction results.

[0070] Step 120: Traverse each line of code in the filtered source code and check if the target constant name exists in each line of code.

[0071] In this embodiment, multiple constant names to be searched (i.e. target constant names) can be preset, and the presence of target constant names in each line of code can be detected by recognizing the characters in each line of code.

[0072] Step 130: Based on the detection results, extract the target constant value corresponding to the target constant name.

[0073] In this step, if a target constant name exists in a line of code, the target constant value specified by the target constant name can be automatically extracted according to the syntax rules of the programming language.

[0074] In this embodiment, by using the header file of the original code text as the data to be processed instead of the source code, the defined constant values ​​can be directly parsed by reading the header file information. Even if the system or scenario corresponding to the constant changes, the user can still arbitrarily replace the header file input for the scenario system without modifying the program code or recompiling the code, which is very user-friendly.

[0075] In one specific embodiment, multiple header file paths and different target constant names can be obtained simultaneously. Then, multi-threading technology is used to execute the above process concurrently, thereby achieving parallel analysis of multiple header files and quickly obtaining all target constant values.

[0076] It should be noted that, in this embodiment, before processing the original code text header file, the comment lines and blank lines can be filtered in advance by traversing the code lines. When relevant personnel have a need to use the header file, the entire filtered text can be traversed line by line again to extract the target constant value; or the code lines in the header file can be traversed only once, and the code filtering operation and the target constant value extraction operation can be performed simultaneously during the traversal. This embodiment does not impose any restrictions on this.

[0077] The technical solution provided by this invention provides a method for analyzing and extracting target constant values ​​from the original code header file, rather than directly obtaining constant values ​​through compilation. This method involves obtaining the header file from the original code text, filtering the comment lines and blank lines in the source code of the header file, traversing each line of code in the filtered source code, detecting whether a target constant name exists in each line of code, and extracting the target constant value corresponding to the target constant name based on the detection result. No matter how the header file changes, it will not affect the source code, effectively avoiding the problem of needing to recompile the code due to frequent modification of constant values, and improving the convenience of the code development process.

[0078] Figure 2 A flowchart of another method for extracting constant values ​​from source code text provided in an embodiment of the present invention is shown below. Figure 2 As shown, the method includes:

[0079] Step 210: Obtain the header file from the original code text, and filter the comment lines and blank lines in the source code of the header file.

[0080] Step 220: Extract one line of code from the filtered source code as the current line of code.

[0081] Step 230: Detect whether the target constant name exists in the current line of code, and establish the target constant flag corresponding to the current line of code based on the detection result.

[0082] In this step, specifically, if the target constant name exists in the current line of code, the target constant flag_find_target is set to true; if the target constant name does not exist in the current line of code, the target constant flag_find_target is set to false.

[0083] Step 240: Determine if the target constant flag corresponding to the current line of code is true. If yes, proceed to step 250. If no, return to the operation of step 220 until the processing of all source code is completed.

[0084] In this embodiment, if the target constant flag is false, the operation of sequentially obtaining one line of code from the filtered source code as the current line of code is returned until the processing of all source code is completed.

[0085] Step 250: Extract the target constant value corresponding to the target constant name in the current line of code.

[0086] In this embodiment, extracting the target constant value corresponding to the target constant name in the current line of code includes: determining whether there are square brackets after the target constant name in the current line of code; if so, determining that the target constant value corresponding to the target constant name is an array and extracting the array; if not, determining that the target constant value corresponding to the target constant name is a single value and extracting the single value.

[0087] Specifically, it can be determined whether there is an "[" after the name of the target constant. If there is no "[", the target constant value is a single value. If there is one "[", the target constant value is a one-dimensional array. If there are two or three "[", the target constant value is a two-dimensional array or a three-dimensional array.

[0088] In this embodiment, if the target constant value is a single value, that is, the target constant name and the target constant value should appear in the same line of code, the target constant value can be extracted according to the programming language syntax rules.

[0089] For example, in C language, if the current line of code is formatted as #define constant_name value, then the characters after the space in the target constant name can be extracted and converted into double numbers; if the current line of code is formatted as doubleconstant_name=value, then the characters after the “=” can be extracted and converted into double numbers.

[0090] In this embodiment, extracting the array includes: replacing preset characters in the current code line with spaces; the preset characters include at least one of the following: equal sign, comma, semicolon, and parentheses; dividing the current code line into string arrays according to spaces; and converting each string into a double number for storage based on the number of the string arrays and the array end marker corresponding to the current code line.

[0091] In a specific embodiment, if the target constant value is a one-dimensional array (e.g., constant_name[4] = {2, 3, 4, 5};), the corresponding characters may be on the same line or not. For example, the "{" character and its value may not be on the same line, the "};" string may not be on the same line as its value, and the values ​​2, 3, 4, 5 may not be on the same line, etc. In this case, this embodiment proposes a method to convert each string into a double number for storage based on the number of strings in the string array and the array end flag corresponding to the current line of code, as follows:

[0092] First, replace all characters before "=" in the current code line with spaces, and replace "{", "}", ",", and ";" with spaces. Then, divide the current code line into different string groups based on spaces. If the size of the string array is 0, it is determined that the value corresponding to the target constant name is not in the current code line, and the above process is repeated in the next code line. Otherwise, if the number of strings in the string array is not 0, each string in the array is converted to a double number and stored in a new one-dimensional array result. While performing the above steps, if the array termination character "}" exists in the current code line, the array termination flag "flag_finish_deal" is set to true; if the array termination character "}" does not exist in the current code line, the array termination flag "flag_finish_deal" is set to false, and the above process is repeated in the next code line.

[0093] In one embodiment of this example, if the target constant value corresponding to the target constant name is a two-dimensional array, then each of the strings is converted into a double number for storage, including:

[0094] Extract the target value within the second square bracket in the two-dimensional array;

[0095] Convert each string into a double number and store it in the target one-dimensional array;

[0096] If the length of the target one-dimensional array is equal to the target value, then the target one-dimensional array is stored in the target two-dimensional array;

[0097] Return to the operation of converting each of the strings into double numbers and storing them in the target one-dimensional array, until all the values ​​of all rows in the two-dimensional array have been stored.

[0098] In a specific implementation, the two-dimensional array is in the form of `double constant_name[][]`, where the characters within the second square brackets must be positive integers and not empty. Before extracting the two-dimensional array, the characters within the second square brackets need to be extracted, converted to int type, and stored as `dim1`. The processing method for target constant values ​​being two-dimensional arrays is as follows:

[0099] First, replace all characters before "=" in the current line of code with spaces, and replace "{", "}", ",", and ";" with spaces. Then, divide the current line of code into different string groups based on spaces. If the size of the string array is 0, it is determined that the value corresponding to the target constant name is not in the current line of code, and the next line of code is read and the above process is repeated. Otherwise, if the number of strings in the string array is not 0, each string is converted into a double number and stored in the target one-dimensional array tmpVec. When the length of the target one-dimensional array is equal to dim1, it is determined that the value of one row of the two-dimensional array has been stored. The target one-dimensional array tmpVec can be stored in the target two-dimensional array result, and the target one-dimensional array tmpVec is cleared before storing the next row of values. This process continues until the values ​​of all rows in the two-dimensional array have been stored. While performing the above steps, if the array termination character "};" exists in the current line of code, the array termination flag "flag_finish_deal" is set to true; if the array termination character "};" does not exist in the current line of code, the array termination flag "flag_finish_deal" is set to false. Then, the next line of code is read and the above process is repeated.

[0100] In another embodiment of this example, if the target constant value corresponding to the target constant name is a three-dimensional array, then each of the strings is converted into a double number for storage, including:

[0101] Extract the target value within the second and third square brackets of the three-dimensional array;

[0102] Convert each string into a double number and store it in the target one-dimensional array;

[0103] If the length of the target one-dimensional array is equal to the target value within the third square bracket, then the target one-dimensional array is stored in the target two-dimensional array;

[0104] If the length of the target two-dimensional array is equal to the target value within the second square brackets, then the target two-dimensional array is stored in the target three-dimensional array;

[0105] Return to the operation of converting each of the strings into double numbers and storing them in the target one-dimensional array, until all the values ​​of all rows in the three-dimensional array have been stored.

[0106] In a specific embodiment, the three-dimensional array is in the form of double constant_value[][][], where the characters within the second and third square brackets must be positive integers and not empty. Before extracting the three-dimensional array, the characters within the second and third square brackets need to be extracted, converted to int types, and stored as dim2 and dim1. The processing method for the target constant value being a three-dimensional array is as follows:

[0107] First, replace all characters before "=" in the current line of code with spaces, and replace "{", "}", ",", and ";" with spaces. Then, divide the current line of code into different string groups based on spaces. If the size of the string array is 0, it is determined that the value corresponding to the target constant name is not in the current line of code, and the above process is repeated in the next line of code. Otherwise, if the number of strings in the string array is not 0, each string is converted into a double number and stored in the target one-dimensional array tmpVec. When the length of the target one-dimensional array is equal to dim1, the target one-dimensional array tmpVec can be stored in the target two-dimensional array tmp2DimVec. If the length of the target two-dimensional array tmp2DimVec is equal to dim2, the target two-dimensional array tmp2DimVec is stored in the target three-dimensional array result. While performing the above steps, if the array termination character "};" exists in the current line of code, the array termination flag "flag_finish_deal" is set to true; if the array termination character "};" does not exist in the current line of code, the array termination flag "flag_finish_deal" is set to false. Then, the next line of code is read and the above process is repeated.

[0108] The technical solution provided by this invention obtains the header file from the original code text, filters the comment lines and blank lines in the source code of the header file, and sequentially obtains one line of code as the current line of code from the filtered source code. It then checks whether there is a target constant name in the current line of code and establishes a target constant flag corresponding to the current line of code based on the detection result. It determines whether the target constant flag corresponding to the current line of code is true. If so, the target constant value corresponding to the target constant name in the current line of code is extracted. If not, the process returns to the previous step of sequentially obtaining one line of code as the current line of code. This technical approach can effectively avoid the problem of frequent modification of constant values ​​causing the code to need to be recompiled, and improves the convenience of the code development process.

[0109] To better illustrate the technical solutions provided in the embodiments of the present invention, the following implementation methods can be referred to:

[0110] Step 310: Initialize the header files in the obtained raw code text. Figure 3a (For the corresponding method flowchart), this step may include the following steps:

[0111] Get the header file path, specify the target constant name, and read the header file;

[0112] Initialize flag_find_start to false, flag_find_end to true, flag_find_target to false, and flag_finish_deal to false;

[0113] Iterate through the header file line by line, read a new line of code, and initialize flag_find_start to false for the corresponding line of code;

[0114] Set " / / " in the current line and all characters thereafter to whitespace characters.

[0115] Step 320: Filter the comment lines in the header file. Figure 3b (For the corresponding method flowchart), this step may include the following steps:

[0116] Determine if the current line contains both " / *" and "* / ". If so, set the first matched " / *" and "* / " and all characters between them to spaces.

[0117] If not, determine if the current line contains only the string " / *". If so, set flag_find_start to true, set flag_find_end to false, and set " / *" and the characters following it to spaces. This step is to set the string following " / *" in the first line of " / *" and "* / " scenarios that span multiple lines to spaces.

[0118] If not, check if "* / " exists in the current line. If it does, set flag_find_end to true and set "* / " and the characters before it to spaces.

[0119] If not, check if `flag_find_start` and `flag_find_end` are false. If they are, it means that the lines preceding the current line only contain " / *", but not "* / ". In other words, this line is between the lines containing " / *" and "* / ", and should be entirely filled with spaces. This step is to fill all intermediate lines between the start and end lines in scenarios where " / *" and "* / " appear across multiple lines with spaces.

[0120] Step 330: Sequentially check the target constant name for each line of code in the header file, and extract the single value when the target constant value is determined to be a single value. Figure 3c (For the corresponding method flowchart), this step may include the following steps:

[0121] Determine if the current line is entirely spaces or an empty line. If so, return and perform a line-by-line traversal of the header file to process subsequent lines of code.

[0122] If not, check if the target constant name exists in the current line. If it does, set the flag_find_target corresponding to the current line to true.

[0123] If not, check if the flag_find_target corresponding to the current line is false. If it is, return to perform the operation of traversing the header file line by line.

[0124] If not, then check if there is no "[" after the target constant name. If there is no "[" after the target constant name, then the target constant value is determined to be a single value.

[0125] If the target constant value is determined to be a single value, check if the current line starts with #define. If it does, extract the string after the target constant name and convert it to double for storage.

[0126] If not, check if the current line contains "=". If it does, extract the string after "=" and convert it to double for storage.

[0127] If there is no #define or "=", an error is returned.

[0128] Step 340: If the target constant value in the line of code is a one-dimensional array, then extract the one-dimensional array. Figure 3d (For the corresponding method flowchart), this step may include the following steps:

[0129] If there is a "[" after the name of the target constant in the current line, it is determined that the target constant value is a one-dimensional array. Then, it is determined whether the flag_finish_deal corresponding to the current line is true. If it is, it is determined that all target constant values ​​have been found and obtained.

[0130] If not, replace "=" and all characters before it with spaces;

[0131] If the current line contains "};", then set flag_finish_deal to true. At the same time, check if the current line contains "{", "}", ";", or ",". If so, set all of these characters to spaces.

[0132] Split the current line into strings by spaces, convert these strings into double numbers, and store them all in a one-dimensional array result.

[0133] Step 350: If the target constant value in the code line is a two-dimensional array, then extract the two-dimensional array. Figure 3e (For the corresponding method flowchart), this step may include the following steps:

[0134] If there are two "[" after the name of the target constant in the current line, it is determined that the target constant value is a two-dimensional array. Then, it is determined that the flag_finish_deal corresponding to the current line is true. If so, it is determined that all target constant values ​​have been found and obtained.

[0135] If not, extract the string within the second "[]" after the target constant name, convert it to an int type dim1, and replace "=" and all characters before it with spaces;

[0136] If the current line contains "};", then set flag_finish_deal to true. At the same time, check if the current line contains "{", "}", ";", or ",". If so, set all of these characters to spaces.

[0137] Split the current line into spaces, convert each string in the string array to a double number, and use a for loop to prepare each double number to be stored in a one-dimensional array tmpVec;

[0138] Determine if the length of the one-dimensional array tmpVec is equal to dim1. If it is, it means that tmpVec has been filled with the value of a row. Store tmpVec in the two-dimensional array result, and then clear tmpVec to prepare to store the value of the next row.

[0139] If not, check if all double numbers in the current row have been traversed. If not, get a new double number and store it in tmpVec. Then return to execute the operation of checking if the length of the one-dimensional array tmpVec is equal to dim1, until all target constant values ​​have been stored.

[0140] Step 360: If the target constant value in the code line is a three-dimensional array, then extract the three-dimensional array. Figure 3f (For the corresponding method flowchart), this step may include the following steps:

[0141] If there are three "[" after the name of the target constant in the current line, it is determined that the target constant value is a three-dimensional array. Then, it is determined that the flag_finish_deal corresponding to the current line is true. If so, it is determined that all target constant values ​​have been found and obtained.

[0142] If not, extract the string within the second and third "[]" after the target constant name character, and convert them to int type dim2 and dim1 respectively, setting "=" and all characters before it to space characters; where dim2 is the second lowest dimension of the three-dimensional array, and dim1 is the lowest dimension of the three-dimensional array;

[0143] If the current line contains "};", then set flag_finish_deal to true. At the same time, check if the current line contains "{", "}", ";", or ",". If so, set all of these characters to spaces.

[0144] Split the current line into spaces, convert each string in the string array to a double number, and use a for loop to prepare each double number to be stored in a one-dimensional array tmpVec;

[0145] Determine if the length of the one-dimensional array tmpVec is equal to dim1. If so, determine that tmpVec has been filled with the value of a row, and store tmpVec into the two-dimensional array tmp2DimVec. Then clear tmpVec to prepare to store the value of the next row.

[0146] Determine if the length of the two-dimensional array tmp2DimVec is equal to dim2. If so, determine that tmp2DimVec has been filled with a set of values, and store tmp2DimVec into the three-dimensional array result. Then empty tmp2DimVec to prepare for storing the next set of values.

[0147] Check if all double numbers in the current row have been traversed. If not, get a new double number and store it in tmpVec. Then return to perform the operation of checking if the size of the one-dimensional array tmpVec is equal to dim1, until all target constant values ​​have been stored.

[0148] Finally, check if all lines of code in the header file's source code text have been traversed. If yes, the method execution is considered complete; otherwise, return to perform the line-by-line traversal of the header file.

[0149] The technical solution of this invention can effectively avoid the problem of code recompilation caused by frequent modification of constant values, thus improving the convenience of the code development process.

[0150] Figure 4 This is a schematic diagram of a constant value extraction device for source code text provided in an embodiment of the present invention. The device is applied in electronic devices, such as... Figure 4 As shown, the device includes: a code filtering module 410, a name detection module 420, and a constant value extraction module 430.

[0151] The code filtering module 410 is used to obtain the header file in the original code text and filter the comment lines and blank lines of the source code in the header file;

[0152] The name detection module 420 is used to traverse each line of code in the filtered source code and detect whether the target constant name exists in each line of code.

[0153] The constant value extraction module 430 is used to extract the target constant value corresponding to the target constant name based on the detection results.

[0154] The technical solution provided by this invention provides a method for analyzing and extracting target constant values ​​from the original code header file, rather than directly obtaining constant values ​​through compilation. This method involves obtaining the header file from the original code text, filtering the comment lines and blank lines in the source code of the header file, traversing each line of code in the filtered source code, detecting whether a target constant name exists in each line of code, and extracting the target constant value corresponding to the target constant name based on the detection result. No matter how the header file changes, it will not affect the source code, effectively avoiding the problem of needing to recompile the code due to frequent modification of constant values, and improving the convenience of the code development process.

[0155] Based on the above embodiments, the code filtering module 410 includes:

[0156] The comment character detection unit is used to traverse each line of code in the source code, detect whether there are preset comment characters in each line of code, establish a comment identifier corresponding to each line of code based on the detection result, determine the comment text based on the comment identifier, and filter the comment text.

[0157] The name detection module 420 includes:

[0158] The identifier establishment unit is used to sequentially obtain one line of code from the filtered source code as the current line of code; detect whether there is a target constant name in the current line of code, and establish the target constant identifier corresponding to the current line of code based on the detection result; if the target constant identifier is false, then return to the operation of sequentially obtaining one line of code from the filtered source code as the current line of code, until the processing of all source code is completed.

[0159] The constant value extraction module 430 includes:

[0160] The extraction unit is used to extract the target constant value corresponding to the target constant name in the current line of code if the target constant identifier bit is true;

[0161] A constant value determination unit is used to determine whether there are square brackets after the target constant name in the current line of code; if so, the target constant value corresponding to the target constant name is determined to be an array, and the array is extracted; if not, the target constant value corresponding to the target constant name is determined to be a single value, and the single value is extracted.

[0162] An array extraction unit is used to replace preset characters in the current code line with spaces; the preset characters include at least one of the following: equal sign, comma, semicolon, and parentheses; the current code line is divided into string arrays according to spaces; and each string is converted into a double number for storage according to the number of the string arrays and the array end marker corresponding to the current code line.

[0163] A two-dimensional array extraction unit is used to extract the target value within the second square bracket in the two-dimensional array; convert each string into a double number and store it in a target one-dimensional array; if the length of the target one-dimensional array is equal to the target value, then store the target one-dimensional array in a target two-dimensional array; return to execute the operation of converting each string into a double number and storing it in the target one-dimensional array until all the values ​​of all rows in the two-dimensional array have been stored.

[0164] A three-dimensional array extraction unit is used to extract the target values ​​within the second and third square brackets of the three-dimensional array; convert each string into a double number and store it in a target one-dimensional array; if the length of the target one-dimensional array is equal to the target value within the third square bracket, then store the target one-dimensional array in a target two-dimensional array; if the length of the target two-dimensional array is equal to the target value within the second square bracket, then store the target two-dimensional array in a target three-dimensional array; return to execute the operation of converting each string into a double number and storing it in the target one-dimensional array, until all values ​​of all rows in the three-dimensional array have been stored.

[0165] The above-described apparatus can execute the methods provided in all the foregoing embodiments of the present invention, and has the corresponding functional modules and beneficial effects for executing the above methods. Technical details not described in detail in the embodiments of the present invention can be found in the methods provided in all the foregoing embodiments of the present invention.

[0166] Figure 5 A schematic diagram of an electronic device 10 that can be used to implement embodiments of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices (e.g., helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.

[0167] like Figure 5 As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12 or a random access memory (RAM) 13, communicatively connected to the at least one processor 11. The memory stores computer programs executable by the at least one processor. The processor 11 can perform various appropriate actions and processes based on the computer program stored in the ROM 12 or loaded from storage unit 18 into the RAM 13. The RAM 13 may also store various programs and data required for the operation of the electronic device 10. The processor 11, ROM 12, and RAM 13 are interconnected via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0168] Multiple components in electronic device 10 are connected to I / O interface 15, including: input unit 16, such as keyboard, mouse, etc.; output unit 17, such as various types of displays, speakers, etc.; storage unit 18, such as disk, optical disk, etc.; and communication unit 19, such as network card, modem, wireless transceiver, etc. Communication unit 19 allows electronic device 10 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0169] Processor 11 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 11 performs the various methods and processes described above, such as the constant value extraction method from source code text.

[0170] In some embodiments, the method for extracting constant values ​​from source code text can be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program can be loaded and / or installed on electronic device 10 via ROM 12 and / or communication unit 19. When the computer program is loaded into RAM 13 and executed by processor 11, one or more steps of the method for extracting constant values ​​from source code text described above can be performed. Alternatively, in other embodiments, processor 11 can be configured to perform the method for extracting constant values ​​from source code text by any other suitable means (e.g., by means of firmware).

[0171] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0172] Computer programs used to implement the methods of the present invention can be written in any combination of one or more programming languages. These computer programs can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs can be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0173] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.

[0174] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0175] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or computing systems that include middleware components (e.g., application servers), or computing systems that include frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.

[0176] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through a communication network. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system to address the shortcomings of traditional physical hosts and VPS services, such as high management difficulty and weak business scalability.

[0177] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.

[0178] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A method for extracting constant values ​​from source code text, characterized in that, The method includes: Obtain the header file from the original code text, and filter the comment lines and blank lines in the source code of the header file; In the filtered source code, each line of code is traversed to check if the target constant name exists in each line of code; Based on the detection results, the target constant values ​​corresponding to the target constant names are extracted.

2. The method according to claim 1, characterized in that, Filtering the comment lines in the source code of the header file, including: The source code is traversed line by line to check if there are any preset comment characters in each line of code; Based on the detection results, a comment identifier is established for each line of code. The comment text is then determined based on the comment identifier and filtered.

3. The method according to claim 1, characterized in that, The filtered source code is traversed line by line to check if the target constant name exists in each line. Based on the detection results, the target constant value corresponding to the target constant name is extracted, including: Extract one line of code from the filtered source code and use it as the current line of code. Check if the target constant name exists in the current line of code, and establish the target constant flag corresponding to the current line of code based on the detection result; If the target constant flag is true, then the target constant value corresponding to the target constant name in the current line of code is extracted; If the target constant flag is false, then return to the operation of sequentially retrieving one line of code from the filtered source code as the current line of code, until the processing of all source code is completed.

4. The method according to claim 3, characterized in that, Extract the target constant value corresponding to the target constant name in the current line of code, including: Determine whether square brackets exist after the target constant name in the current line of code; If so, then determine that the target constant value corresponding to the target constant name is an array, and extract the array; If not, then the target constant value corresponding to the target constant name is determined to be a single value, and the single value is extracted.

5. The method according to claim 4, characterized in that, Extracting the array includes: Replace the preset characters in the current line of code with spaces; the preset characters include at least one of the following: equal sign, comma, semicolon, and parentheses; Divide the current line of code into a string array based on spaces; Based on the number of strings in the string array and the array end marker corresponding to the current line of code, each string is converted into a double number for storage.

6. The method according to claim 5, characterized in that, If the target constant value corresponding to the target constant name is a two-dimensional array, then each of the strings will be converted to a double number for storage, including: Extract the target value within the second square bracket in the two-dimensional array; Convert each string into a double number and store it in the target one-dimensional array; If the length of the target one-dimensional array is equal to the target value, then the target one-dimensional array is stored in the target two-dimensional array; Return to the operation of converting each of the strings into double numbers and storing them in the target one-dimensional array, until all the values ​​of all rows in the two-dimensional array have been stored.

7. The method according to claim 5, characterized in that, If the target constant value corresponding to the target constant name is a three-dimensional array, then each of the strings will be converted to a double number for storage, including: Extract the target value within the second and third square brackets of the three-dimensional array; Convert each string into a double number and store it in the target one-dimensional array; If the length of the target one-dimensional array is equal to the target value within the third square bracket, then the target one-dimensional array is stored in the target two-dimensional array; If the length of the target two-dimensional array is equal to the target value within the second square brackets, then the target two-dimensional array is stored in the target three-dimensional array; Return to the operation of converting each of the strings into double numbers and storing them in the target one-dimensional array, until all the values ​​of all rows in the three-dimensional array have been stored.

8. A device for extracting constant values ​​from source code text, characterized in that, The device includes: The code filtering module is used to obtain the header file in the original code text and filter the comment lines and blank lines in the source code of the header file; The name detection module is used to traverse each line of code in the filtered source code and detect whether the target constant name exists in each line of code; The constant value extraction module is used to extract the target constant value corresponding to the target constant name based on the detection results.

9. An electronic device, characterized in that, The electronic device includes: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the constant value extraction method of the source code text according to any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that cause a processor to execute the constant value extraction method of the source code text according to any one of claims 1-7.