A code amount counting method, system, storage medium and device
By adding separators to lines of code and recognizing comment characters, the insufficient accuracy and adaptability of existing line count statistics are solved, achieving high-precision code count statistics.
Patent Information
- Application Number
- CN202611069355.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-17
- Publication Date
- 2026-11-24
AI Technical Summary
Existing methods for counting lines of code suffer from insufficient accuracy, are time-consuming and labor-intensive, are prone to errors, and cannot effectively distinguish between comments and code, especially in multilingual environments where accurate analysis is difficult.
By adding a unique separator to each line of code and combining the recognition of single-line and multi-line comment characters, valid code text is extracted, enabling accurate statistics for different programming languages.
It achieves high-precision statistics on lines of code, accurately distinguishing between valid code, comment lines, and blank lines, and is compatible with multiple programming languages, thus improving the accuracy and efficiency of the statistics.
Smart Images

Figure CN123157247A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of software engineering technology, and in particular relates to a code volume statistics method, system, storage medium and device. Background Technology
[0002] In the field of software engineering, quantitatively assessing the size of a project's codebase is a fundamental and crucial task, essentially involving the counting of effective lines of code in the source code files. Line count statistics, as a key indicator for measuring software complexity, assessing development workload, estimating project progress, and tracking project schedules, play a vital role in project management, cost accounting, and quality control. However, current technical solutions face a series of inherent challenges and limitations in practical applications.
[0003] Currently, commonly used statistical methods include disk space estimation, code commit log integration, manual statistics, and tool-based statistics. Space estimation has poor accuracy and cannot evaluate commented code; code commit logs require merging with log information submitted by designers, and errors in this information can lead to statistical errors; manual statistics are time-consuming, labor-intensive, difficult to scale, and prone to errors; tool-based statistics are convenient and fast, but have requirements regarding file type display and cannot effectively analyze cases with multiple commented lines or a mixture of valid code and comments.
[0004] Therefore, there is an urgent need for a code volume statistics technology that can be cross-language, highly accurate, and high-performance, in order to overcome the shortcomings of existing methods and provide a more reliable and detailed quantitative analysis basis for software engineering practices. Summary of the Invention
[0005] To address the aforementioned issues, this application provides a code volume counting method, system, storage medium, and device for counting the number of lines in source code files of various programming languages. It can accurately distinguish between valid code lines, pure comment lines, blank lines, and mixed lines (where code and comments coexist), and correctly parse mixed lines, thereby obtaining a valid number of code lines that truly reflects the workload and improving the accuracy of code volume counting.
[0006] This application is achieved through the following technical solution: The target files to be analyzed are read from the list of files to be tested, the programming language of the target files is determined, and the corresponding annotation syntax rules are loaded. Based on the aforementioned annotation syntax rules, the target file is parsed to count the total number of lines and the number of blank lines, and the effective code text is extracted. Based on the valid code text, the number of lines is calculated and summarized to obtain the total number of lines, valid code lines, blank lines, and comment lines.
[0007] Optional, The process of reading the target files to be analyzed based on the list of files to be tested, determining the programming language of the target files, and loading the corresponding annotation syntax rules includes: Read the list of files to be tested and preprocess them to obtain a valid list of file paths to be analyzed. File path detection and confirmation are performed based on the list of file paths to be analyzed. The programming language of the current file is determined based on the file extension, and the corresponding comment syntax rules are loaded. The comment syntax rules include single-line comment characters and multi-line comment character pairs.
[0008] Optional, Based on the aforementioned annotation syntax rules, the target file is parsed to count the total number of lines and the number of blank lines, and valid code text is extracted, including: Read the target file and obtain the total number of lines; Process the file line by line and record the number of blank lines; Remove leading and trailing whitespace and newline characters from non-whitespace lines, and add a globally unique line separator at the end of the line; Concatenate all non-blank lines to obtain the identifier text; Enter the inner loop and perform iterative annotation stripping based on the identified text to extract valid code text.
[0009] Optional, The process of entering the inner loop involves iterative annotation stripping based on the identified text to extract valid code text, including: Find the first comment character based on the identified text: If a single-line comment character is found first, the content from the beginning of the identifier text to the single-line comment character and the separator of the line containing the single-line comment character are appended to the valid code text; the identifier text is updated to the content after the separator of the line containing the single-line comment character; the first comment character is found based on the updated identifier text. If a multi-line comment start character is found first, the portion of the identifier text from the beginning of the multi-line comment start character to the beginning of the multi-line comment start character is appended to the valid code text; the adjacent multi-line comment end character after the multi-line comment start character is found, and the identifier text is updated to the content after the multi-line comment end character; all separators between the multi-line comment start character and the multi-line comment end character are added to the valid code text; the first comment character is found based on the updated identifier text; If no comment character is found, the entire identifier text is appended to the valid code text, and the loop ends.
[0010] Optional, The calculation and summarization of line counts based on the valid code text, to obtain the total number of lines, valid code lines, blank lines, and comment lines, includes: Based on the valid code text, count the number of valid code lines; Calculate the number of comment lines based on the total number of lines, the number of blank lines, and the number of valid lines of code; Record the total number of lines, the number of valid lines of code, the number of blank lines, and the number of comment lines to complete the statistics.
[0011] Optional, Based on the aforementioned valid code text, count the valid code lines, including: The valid code text is split into multiple segments according to the delimiter; Iterate through each segment and mark non-empty segments as valid lines of code; Count the number of valid lines of code.
[0012] Optional, Process all files in the list of files to be tested and output the statistical results to a table.
[0013] This application also provides a code volume statistics system for implementing the aforementioned method, the system comprising: The file reading module is used to read the target files to be analyzed based on the list of files to be tested. The syntax rule configuration module is used to determine the programming language of the target file and load the corresponding annotation syntax rules; The parsing module is used to parse the target file based on the annotation syntax rules, count the total number of lines and the number of blank lines, and extract the valid code text; The statistics module is used to calculate and summarize the number of lines based on the valid code text, and to obtain the total number of lines, valid code lines, blank lines, and comment lines.
[0014] This application also provides a computer-readable storage medium storing one or more programs, which, when executed, can implement the aforementioned code volume statistics method.
[0015] This application also provides a device, including a processor, a communication interface, a computer-readable storage medium, and a communication bus; wherein the processor, the communication interface, and the computer-readable storage medium communicate with each other through the communication bus; The processor is used to execute programs stored in a computer-readable storage medium.
[0016] Compared with the prior art, this application has the following advantages: 1. The code volume statistics method proposed in this application distinguishes each line by adding a unique separator to each line, and integrates the file content into a single-line string for subsequent analysis; by identifying the order of appearance of single-line comments and multi-line comments, it can extract effective code text, accurately distinguish code lines from comment lines, and achieve accurate statistics of code volume, avoiding the tediousness of manual statistics and the inaccuracy of tool statistics.
[0017] 2. It performs precise analysis and output on the input file list and file content. Through lexical analysis-level comment stripping, rather than simple line beginning matching, it can accurately handle complex situations such as mixed lines and multi-line comments. It can accurately detect cases where valid code, single-line comment characters, and multi-line comment characters are mixed, especially the case where multi-line comments are mixed with valid code, thus improving the accuracy of statistics.
[0018] 3. By dynamically loading comment rules through file extensions, it can easily extend support for multiple programming languages such as C, C++, Java, Python, Verilog, VHDL, SystemVerilog, and MATLAB, improving adaptability to files in different languages.
[0019] Other features and advantages of this application will be set forth in the description which follows, and will be apparent in part from the description, or may be learned by practicing the application. The objectives and other advantages of this application may be realized and obtained by means of the structures pointed out in the description, claims and drawings. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0021] Figure 1 A flowchart illustrating the code volume statistics method is shown; Figure 2 This paper illustrates a schematic diagram of the specific implementation process of the code volume statistics method according to an embodiment of this application; Figure 3 This illustration shows a schematic diagram of the document content parsing and conversion process according to an embodiment of this application; Figure 4 A schematic block diagram of the code quantity statistics system is shown. Figure 5 This is a schematic diagram of the structure of a device according to an embodiment of this application. Detailed Implementation
[0022] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0023] The present application will now be described in further detail with reference to the accompanying drawings and specific embodiments.
[0024] See appendix Figure 1 and 2 The method of this application includes: S1. Read the target files to be analyzed based on the list of files to be tested, determine the programming language of the target files, and load the corresponding annotation syntax rules.
[0025] This includes reading the target files to be analyzed based on the list of files to be tested, determining the programming language of the target files, and loading the corresponding annotation syntax rules, including: Read the list of files to be tested and preprocess them to obtain a valid list of file paths to be analyzed. File path detection and confirmation are performed based on the list of file paths to be analyzed. The programming language of the current file is determined based on the file extension, and the corresponding comment syntax rules are loaded. The comment syntax rules include single-line comment characters and multi-line comment character pairs.
[0026] In this embodiment, the list of files to be tested is read, and blank lines and lines starting with comments are filtered out to obtain a valid list of file paths to be analyzed. For all content in the valid file list, each file path is checked to see if it exists: if it does not exist, the process terminates or an error is recorded; otherwise, the single file analysis process begins. Based on the target file extension (e.g., .c, .cpp, .v, .vhd), the programming language of the current file is determined, and the corresponding comment syntax rules are loaded, including single-line comment characters (e.g., / / or #) and multi-line comment character pairs (e.g., / ). and / ).
[0027] S2. Based on the annotation syntax rules, parse the content of the target file, count the total number of lines and the number of blank lines, and extract the valid code text.
[0028] Specifically, based on the aforementioned annotation syntax rules, the target file is parsed to count the total number of lines and the number of blank lines, and valid code text is extracted, including: Read the target file and obtain the total number of lines; Process the file line by line and record the number of blank lines; Remove leading and trailing whitespace and newline characters from non-whitespace lines, and add a globally unique line separator at the end of the line; Concatenate all non-blank lines to obtain the identifier text; Enter the inner loop and perform iterative annotation stripping based on the identified text to extract valid code text.
[0029] In this embodiment, the inner loop is entered, and iterative annotation stripping is performed based on the identified text to extract valid code text, including: Find the first comment character based on the identified text: If a single-line comment character is found first, the content from the beginning of the identifier text to the single-line comment character and the separator of the line containing the single-line comment character are appended to the valid code text; the identifier text is updated to the content after the separator of the line containing the single-line comment character; the first comment character is found based on the updated identifier text. If a multi-line comment start character is found first, the portion of the identifier text from the beginning of the multi-line comment start character to the beginning of the multi-line comment start character is appended to the valid code text; the adjacent multi-line comment end character after the multi-line comment start character is found, and the identifier text is updated to the content after the multi-line comment end character; all separators between the multi-line comment start character and the multi-line comment end character are added to the valid code text; the first comment character is found based on the updated identifier text; If no comment character is found, the entire identifier text is appended to the valid code text, and the loop ends.
[0030] like Figure 3 As shown, in this embodiment, based on the annotation syntax rules, the specific process of parsing and converting the target file, counting the total number of lines and the number of blank lines, and extracting valid code text includes: Read the contents of the target file and obtain the total number of lines L_total; Initialize the blank line counter L_blank to 0 and the valid code line counter L_code to 0; Process the file line by line: If a blank line is detected, L_blank is incremented by 1; Otherwise, remove leading and trailing whitespace and newline characters, and add a globally unique line separator (hereinafter referred to as "separator") at the end of the line. Ensure that this separator does not appear in all source code, such as <<<LINE_SEP> >>); Concatenate all non-blank lines using the delimiters mentioned above (that is, convert multi-line code into a string format of single-line code containing multiple delimiters) to generate "identifier text"; Iterative annotation stripping to extract valid code: Initialize an empty "valid code text" string to record the valid code extracted after removing whitespace and comments from the identifier text; enter the inner loop to perform iterative annotation stripping on the identifier text until the process is complete, and obtain the valid code text of the target file.
[0031] The specific process of performing iterative annotation stripping to extract valid code from the identified text includes: Based on the identifier text, find the first comment character: Start searching from the beginning of the identifier text, simultaneously looking for the first occurrence of a single-line comment character (such as / / ) and a multi-line comment start character (such as / ). The position of ) can result in one of the following three situations: a) Case 1: Single-line comments take precedence. If only a single-line comment character is found, or the position of the single-line comment character is earlier than the position of the multi-line comment start character, then: The portion of the identifier text from the beginning to the single-line comment character, along with the separator of the line containing the single-line comment character, is appended to the valid code text; The updated identifier text is: find the first delimiter in the content after the current single-line comment character, and take the part after it.
[0032] This operation skips all the remaining content of the original line (including comments and any subsequent code, but by definition, the code on the same line after a single-line comment symbol is invalid), achieving precise removal of single-line comments.
[0033] b) Scenario 2: Multiline comments take precedence. If only a multiline comment start symbol is found, or its position is earlier than that of a single-line comment symbol, then: Append the portion of the identifier text from the beginning of the multi-line comment to the valid code text; In the identified text, starting from after the current multi-line comment start character, search for the first multi-line comment end character (e.g., ...). / ); The updated identifier text is: the content following the found multi-line comment terminator; Considering the possibility that valid code may exist before the start symbol and after the end symbol of a multi-line comment, to prevent it from being misidentified as a single line, for multi-line comments, the separator at the end of each line is added to the valid text (Note: Each set of separators is directly adjacent, so this behavior will not affect the subsequent parsing of the number of valid code lines).
[0034] c) Case 3: No comment. If no comment character is found, append the entire identifier text to the valid code text, and then exit the loop.
[0035] At this point, the parsing and conversion of the target file content is complete, and valid code text is obtained.
[0036] S3. Calculate and summarize the number of lines based on the valid code text, and obtain the total number of lines, valid code lines, blank lines and comment lines.
[0037] The calculation and summarization of line counts based on the valid code text yields the total number of lines, valid code lines, blank lines, and comment lines, including: Based on the valid code text, count the valid lines of code; Calculate the number of comment lines based on the total number of lines, the number of blank lines, and the number of valid code lines; Record the total number of lines, valid lines of code, blank lines, and comment lines to complete the statistics.
[0038] In this embodiment, the valid code text is segmented according to delimiters. Each segment is traversed, and if the segment is not empty, the valid code line counter L_code is incremented by 1. The number of comment lines is calculated as: L_comment = L_total - L_code - L_blank. At this point, the four metrics (total number of lines, valid code lines, blank lines, and comment lines) for a single file are counted and recorded.
[0039] In this embodiment, the above steps are repeated to process all files in the file list. An Excel spreadsheet is created to automatically output the summary results for easy and intuitive display. Optionally, if historical statistical results are available, the current statistical results can be compared with them, and any differences and their results can be displayed and annotated in the table.
[0040] To facilitate understanding, the following example is used for illustration: One of the Verilog source files in the file list, test.v, contains the following:
[0041] After removing blank lines, adding a separator (set to %%% for clarity), and merging them into one line, the identifier text will be: module top;%%%reg a;%%%reg b; / / aaa%%% / bbb%%% / / ccc%%%ddd / %%% / / / / / %%% / eee / / / %%%reg c; / fff / %%%reg d; / ggg / %%% / hhh%%% iii / / jjj%%% / rege;%%% / kkk / reg f;%%% / lll / reg g; / / / %%%mmm%%% / %%%reg h; / / / %%%regi; / %%%nnn%%% / reg j;%%%endmodule%%% Extract the valid code text as follows: module top;%%%reg a;%%%reg b;%%%%%%%%%%%%%%%%reg c;%%%regd;%%%%%%%%reg e;%%% reg f;%%% reg g;%%%%%%%%reg h;%%%reg i;%%%%%%reg j;%%%endmodule%%% After splitting it by delimiters, the number of valid lines of code can be parsed out, and then all statistical results can be obtained.
[0042] The output table will be in the following format (test2.v is a virtual file, used only to show the totals):
[0043] like Figure 4 As shown in the figure, this application embodiment also provides a code volume statistics system, including: a file reading module, a syntax rule configuration module, a parsing module and a statistics module.
[0044] The file reading module is used to read the target files to be analyzed based on the list of files to be tested. The syntax rule configuration module is used to determine the programming language of the target file and load the corresponding annotation syntax rules; The parsing module is used to parse the target file based on the annotation syntax rules, count the total number of lines and the number of blank lines, and extract the valid code text; The statistics module is used to calculate and summarize the number of lines based on the valid code text, and to obtain the total number of lines, valid code lines, blank lines, and comment lines.
[0045] like Figure 5As shown in the illustration, this application also provides a device including a processor, a communication interface, a memory, and a communication bus. The processor, communication interface, and memory communicate with each other via the communication bus.
[0046] The memory is a computer-readable storage medium used to store one or more programs.
[0047] The processor is configured to execute a program stored in a computer-readable storage medium.
[0048] The computer-readable storage medium may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device.
[0049] Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A code amount counting method characterized by comprising: include: The target files to be analyzed are read from the list of files to be tested, the programming language of the target files is determined, and the corresponding annotation syntax rules are loaded. Based on the aforementioned annotation syntax rules, the target file is parsed to count the total number of lines and the number of blank lines, and the effective code text is extracted. Based on the valid code text, the number of lines is calculated and summarized to obtain the total number of lines, valid code lines, blank lines, and comment lines.
2. The method of claim 1, wherein, The process of reading the target files to be analyzed based on the list of files to be tested, determining the programming language of the target files, and loading the corresponding annotation syntax rules includes: Read the list of files to be tested and preprocess them to obtain a valid list of file paths to be analyzed. File path detection and confirmation are performed based on the list of file paths to be analyzed. The programming language of the current file is determined based on the file extension, and the corresponding comment syntax rules are loaded. The comment syntax rules include single-line comment characters and multi-line comment character pairs.
3. The method of claim 1, wherein, Based on the aforementioned annotation syntax rules, the target file is parsed to count the total number of lines and the number of blank lines, and valid code text is extracted, including: Read the target file and obtain the total number of lines; Process the file line by line and record the number of blank lines; Remove leading and trailing whitespace and newline characters from non-whitespace lines, and add a globally unique line separator at the end of the line; Concatenate all non-blank lines to obtain the identifier text; Enter the inner loop and perform iterative annotation stripping based on the identified text to extract valid code text.
4. The method according to claim 3, characterized in that, The process of entering the inner loop involves iterative annotation stripping based on the identified text to extract valid code text, including: Find the first comment character based on the identified text: If a single-line comment character is found first, the content from the beginning of the identifier text to the single-line comment character and the separator of the line containing the single-line comment character are appended to the valid code text; the identifier text is updated to the content after the separator of the line containing the single-line comment character; the first comment character is found based on the updated identifier text. If a multi-line comment start character is found first, the portion of the identifier text from the beginning of the multi-line comment start character to the beginning of the multi-line comment start character is appended to the valid code text; the adjacent multi-line comment end character after the multi-line comment start character is found, and the identifier text is updated to the content after the multi-line comment end character; all separators between the multi-line comment start character and the multi-line comment end character are added to the valid code text; the first comment character is found based on the updated identifier text; If no comment character is found, the entire identifier text is appended to the valid code text, and the loop ends.
5. The method according to claim 1, characterized in that, The calculation and summarization of line counts based on the valid code text, to obtain the total number of lines, valid code lines, blank lines, and comment lines, includes: Based on the valid code text, count the number of valid code lines; Calculate the number of comment lines based on the total number of lines, the number of blank lines, and the number of valid lines of code; Record the total number of lines, the number of valid lines of code, the number of blank lines, and the number of comment lines to complete the statistics.
6. The method according to claim 5, characterized in that, Based on the aforementioned valid code text, count the valid code lines, including: The valid code text is split into multiple segments according to the delimiter; Iterate through each segment and mark non-empty segments as valid lines of code; Count the number of valid lines of code.
7. The method according to any one of claims 1-6, characterized in that, Process all files in the list of files to be tested and output the statistical results to a table.
8. A code volume statistics system, characterized in that, The system includes: The file reading module is used to read the target files to be analyzed based on the list of files to be tested. The syntax rule configuration module is used to determine the programming language of the target file and load the corresponding annotation syntax rules; The parsing module is used to parse the target file based on the annotation syntax rules, count the total number of lines and the number of blank lines, and extract the valid code text; The statistics module is used to calculate and summarize the number of lines based on the valid code text, and to obtain the total number of lines, valid code lines, blank lines, and comment lines.
9. A computer-readable storage medium storing one or more programs, characterized in that, When one or more programs are executed, the code volume statistics method described in any one of claims 1-7 can be implemented.
10. An electronic device, comprising a processor, a communication interface, a computer-readable storage medium as described in claim 9, and a communication bus; wherein, The processor, communication interface, and computer-readable storage medium communicate electronically with each other via a communication bus; characterized in that, The processor is used to execute programs stored in a computer-readable storage medium.
