CN109710304A - A kind of Format adjusting method and device - Google Patents

A kind of Format adjusting method and device Download PDF

Info

Publication number
CN109710304A
CN109710304A CN201811609730.6A CN201811609730A CN109710304A CN 109710304 A CN109710304 A CN 109710304A CN 201811609730 A CN201811609730 A CN 201811609730A CN 109710304 A CN109710304 A CN 109710304A
Authority
CN
China
Prior art keywords
character
code
type
format
group
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN201811609730.6A
Other languages
Chinese (zh)
Other versions
CN109710304B (en
Inventor
刘硕
史家涛
李峰
何晓明
潘文卿
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Weichai Power Co Ltd
Original Assignee
Weichai Power Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Weichai Power Co Ltd filed Critical Weichai Power Co Ltd
Priority to CN201811609730.6A priority Critical patent/CN109710304B/en
Publication of CN109710304A publication Critical patent/CN109710304A/en
Application granted granted Critical
Publication of CN109710304B publication Critical patent/CN109710304B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Landscapes

  • Devices For Executing Special Programs (AREA)
  • Stored Programmes (AREA)

Abstract

The present invention provides a kind of Format adjusting method and device, obtain the source code for needing to carry out code format adjustment, all characters in source code are split in the code compilation process of source code, obtain multiple independent component parts, obtain multiple corresponding types of independent component part, based on multiple independent corresponding types of component part, Format adjusting is carried out to source code, obtain the object code with specific format, the specific format that wherein different source codes carries out between the object code obtained after Format adjusting is identical, it realizes and the source code for needing to carry out code format adjustment is subjected to unified Format adjusting, to improve the readability of code, so that later period modification and maintenance improve code reading efficiency, and Character segmentation is carried out in the code compilation process of source code, the defeated of character can be passed through in this way Enter to realize code compilation and code format adjustment while progress, reduces the time-consuming of code compilation and code format adjustment.

Description

A kind of Format adjusting method and device
Technical field
The invention belongs to code process technical fields, more specifically, more particularly to a kind of Format adjusting method and device.
Background technique
With the development of computer industry, software project is more and more, and a software project needs multiple programmer's cooperations Joint development, and the format of the write code of each programmer is different, therefore modifies and safeguard for the ease of the later period, needs to have There is the code of different-format to be adjusted to the code with unified format.
The mode that the code with different-format is adjusted to the code with unified format is had at present: pre-establishing code It format specification and is assisted using code editor, but distinct program person is different for pre-establishing code format specification Understanding similarly using code so that also can be different based on the format of the code that code format specification obtains is pre-established Distinct program person is different to the setting of menu relevant to format in code editor when editing machine, also results in and is compiled by code The format for collecting the code that device provides is different.
Summary of the invention
In view of this, the purpose of the present invention is to provide a kind of Format adjusting method and device, for by different-format Source code is adjusted to the object code with identical specific format.
The present invention provides a kind of Format adjusting method, which comprises
Obtain the source code for needing to carry out code format adjustment;
All characters in the source code are split in the code compilation process of the source code, are obtained multiple Independent component part, obtains the multiple independent corresponding type of component part, in the multiple independent component part Any independent component part is character string or character;
Based on the multiple independent corresponding type of component part, Format adjusting is carried out to the source code, is obtained Object code with specific format, wherein specific between the object code obtained after different source code progress Format adjustings Format is identical.
Preferably, it is based on the multiple independent corresponding type of component part, format tune is carried out to the source code It is whole, obtain having the object code of specific format include:
Based on the multiple independent corresponding type of component part and default adjustment rule, the source code is carried out Format adjusting obtains the object code with specific format, so that different source codes obtains after default adjustment rule adjustment The specific format between object code arrived is identical.
Preferably, described based on the multiple independent corresponding type of component part and default adjustment rule, to institute It states source code and carries out Format adjusting, obtain having the object code of specific format include:
Based on the multiple independent corresponding type of component part, chosen and the type from default adjustment rule Corresponding type adjustment rule;
It is chosen from the default adjustment rule and shares adjustment rule, the shared adjustment rule is for in every line code Part in addition to independent component part carries out format restriction;
Based on selected type adjustment rule and adjustment rule is shared, Format adjusting is carried out to the source code, is obtained Object code with format required by type adjustment rule and shared adjustment rule.
Preferably, described that all characters in the source code are divided in the code compilation process of the source code It cuts, obtains multiple independent component parts, obtaining the multiple independent corresponding type of component part includes:
In the code compilation process of the source code, every line character of the source code is inputted one by one;
The character group that every line character includes is obtained, wherein the character group that any one line character includes has: in the line character Character in character group, the line character between one character and the 1st space between last character and n-th of space Character group between group, i-th of space and i+1 space, wherein the value of i is 2 to n-1, and n, which is in the source code, includes Space sum;
To any character group: determining that the character that the character group includes is character string or word based on the first preset matching rule Symbol, and type is obtained based on the second preset matching rule.
Preferably, described to any character group: the character for determining that the character group includes based on the first preset matching rule is Character string or character, and type is obtained based on the second preset matching rule and includes:
Following steps are executed to any character group:
If the full content that the character group carries is one of number, operator, punctuation mark and default reserved word, Then the type of the character group is the corresponding type of full content, if character quantity determines the character group greater than 1 in the character group Determine that the character group is a character if character quantity is equal to 1 in the character group for a character string;
If the full content that the character group carries does not include number, operator, punctuation mark and default reserved word, it is somebody's turn to do The type of character group is argument types, if character quantity is greater than 1 in the character group, determines that the character group is a character String determines that the character group is a character if character quantity is equal to 1 in the character group;
If character quantity passes through the preset characters greater than 1 and including at least one preset characters in the character group The character group is split, preset characters are determined as to the type phase of a character and the type and preset characters of the character Together, the preset characters include at least one of operator, punctuation mark and number;
To any portion in each section split by the preset characters: if character quantity is equal in the part 1, it is determined that the part is that the type of a character and character is argument types, if character quantity is greater than 1 in the part, Determine that the part is a character string, if the character string is identical as one in default reserved word, it is determined that the character string Type is reserved word type, otherwise determines that the type of the character string is argument types.
The present invention provides a kind of Format adjusting device, and described device includes:
Module is obtained, for obtaining the source code for needing to carry out code format adjustment;
Determination type module, in the code compilation process of the source code to all characters in the source code It is split, obtains multiple independent component parts, obtain the multiple independent corresponding type of component part, it is the multiple Any independent component part in independent component part is character string or character;
Module is adjusted, for being based on the multiple independent corresponding type of component part, the source code is carried out Format adjusting obtains the object code with specific format, wherein different source codes carries out the target obtained after Format adjusting Specific format between code is identical.
Preferably, the adjustment module, for being based on the multiple independent corresponding type of component part and presetting Adjustment rule carries out Format adjusting to the source code, the object code with specific format is obtained, so that different source codes The specific format between object code obtained after default adjustment rule adjustment is identical.
Preferably, the adjustment module includes:
Type adjusts rule determination unit, for being based on the multiple independent corresponding type of component part, from pre- If choosing type adjustment rule corresponding with the type in adjustment rule;
Adjustment rule determination unit is shared, shares adjustment rule for choosing from the default adjustment rule, it is described total With adjustment rule for carrying out format restriction to the part in every line code in addition to independent component part;
Adjustment unit, for being carried out to the source code based on selected type adjustment rule and shared adjustment rule Format adjusting obtains having the type adjustment rule and shares the object code of format required by adjustment rule.
Preferably, the determination type module includes:
Reading unit, in the code compilation process of the source code, one by one by every line character of the source code Input;
Cutting unit, the character group for including for obtaining every line character, wherein the character group that any one line character includes has: Last character and n-th of space in character group, the line character in the line character between first character and the 1st space Between character group, the character group between i-th of space and i+1 space, wherein the value of i is 2 to arrive n-1, and n is the source The space sum for including in code;
Type determining units, for any character group: determining that the character group includes based on the first preset matching rule Character is character string or character, and obtains type based on the second preset matching rule.
Preferably, the type determining units, for executing following steps to any character group:
If the full content that the character group carries is one of number, operator, punctuation mark and default reserved word, Then the type of the character group is the corresponding type of full content, if character quantity determines the character group greater than 1 in the character group Determine that the character group is a character if character quantity is equal to 1 in the character group for a character string;
If the full content that the character group carries does not include number, operator, punctuation mark and default reserved word, it is somebody's turn to do The type of character group is argument types, if character quantity is greater than 1 in the character group, determines that the character group is a character String determines that the character group is a character if character quantity is equal to 1 in the character group;
If character quantity passes through the preset characters greater than 1 and including at least one preset characters in the character group The character group is split, preset characters are determined as to the type phase of a character and the type and preset characters of the character Together, the preset characters include at least one of operator, punctuation mark and number;
To any portion in each section split by the preset characters: if character quantity is equal in the part 1, it is determined that the part is that the type of a character and character is argument types, if character quantity is greater than 1 in the part, Determine that the part is a character string, if the character string is identical as one in default reserved word, it is determined that the character string Type is reserved word type, otherwise determines that the type of the character string is argument types.
According to the above-mentioned technical solution, the source code for needing to carry out code format adjustment is obtained, in the code of source code All characters in source code are split in compilation process, obtain multiple independent component parts, obtain multiple independent compositions The corresponding type in part is based on multiple independent corresponding types of component part, carries out Format adjusting to source code, obtains To the object code with specific format, wherein different source codes carries out the spy between the object code obtained after Format adjusting The formula that fixes is identical, realizes the source code for needing to carry out code format adjustment carrying out unified Format adjusting, to improve code Readability, and unified format can allow each programmer to be capable of the thinking and purpose of accurate understanding code, think in accurate understanding Code can accurately be changed on the basis of road and purpose, to be conducive to the modification and maintenance of code.And character point Cutting is carried out in the code compilation process of source code, can realize code compilation and code by the input of a character in this way It is carried out while Format adjusting, reduces the time-consuming of code compilation and code format adjustment.
Detailed description of the invention
In order to more clearly explain the embodiment of the invention or the technical proposal in the existing technology, to embodiment or will show below There is attached drawing needed in technical description to be briefly described, it should be apparent that, the accompanying drawings in the following description is the present invention Some embodiments for those of ordinary skill in the art without creative efforts, can also basis These attached drawings obtain other attached drawings.
Fig. 1 is a kind of flow chart of Format adjusting method provided in an embodiment of the present invention;
Fig. 2 is a kind of structural schematic diagram of Format adjusting device provided in an embodiment of the present invention.
Specific embodiment
In order to make the object, technical scheme and advantages of the embodiment of the invention clearer, below in conjunction with the embodiment of the present invention In attached drawing, technical scheme in the embodiment of the invention is clearly and completely described, it is clear that described embodiment is A part of the embodiment of the present invention, instead of all the embodiments.Based on the embodiments of the present invention, those of ordinary skill in the art Every other embodiment obtained without making creative work, shall fall within the protection scope of the present invention.
Referring to Fig. 1, it illustrates a kind of flow chart of Format adjusting method provided in an embodiment of the present invention, for need The source code for carrying out code format adjustment carries out Format adjusting, the object code with specific format is obtained, specifically, Fig. 1 Shown Format adjusting method may comprise steps of:
S101: the source code for needing to carry out code format adjustment is obtained, it is possible to understand that are as follows: the code that each programmer writes Format it is different, in order to improve the readability of code, so that code structure is more beautiful, need to source code (i.e. The code that programmer writes) code format adjustment is carried out, it can thus be appreciated that the source code for carrying out code format adjustment is needed to can be But it is not limited to without Format adjusting and with the source code of Format adjusting demand.
Wherein, obtain and need a kind of mode of the source code for carrying out code format adjustment to be: programmer passes through compiler volume The code write, another way are: being written by the text codes of copy editor's software write, then by text codes to compiling The code of device.
It should be noted that above-mentioned source code needs to meet the preparation request of compiler, i.e., code is being write in programmer When, it needs to abide by redaction rule, avoids occurring compile error in code compilation process, and make in subsequent determining fractionation Occurs mistake when character.
S102: being split all characters in source code in the code compilation process of source code, obtains multiple only Vertical component part, obtains multiple independent corresponding types of component part, wherein any only in multiple independent component parts Vertical component part is character or character string.
That is for source code, which independently forms portion by character string and character both types It is grouped as, the stage that the present embodiment obtains multiple independent component parts is in code compilation process, such as in the word of code compilation The method analysis phase is analyzed this is because needing to input character one by one in the lexical analysis phase, and is obtaining independent composition portion Timesharing is also required to input character one by one, it is possible to be split in the lexical analysis phase, realize that a character input can obtain To multiple independent component parts and morphology is analyzed, so as to reduce character input number, improves code compilation and generation The time-consuming of code Format adjusting.
In the present embodiment, a kind of feasible pattern for obtaining multiple independent corresponding types of component part is: in source In the code compilation process of code, every line character of source code is inputted one by one, obtains the character group that every line character includes, wherein The character group that any one line character includes has: character group, the row word in the line character between first character and the 1st space Character group, i-th of space in symbol between last character and n-th of space and the character group between i+1 space, i Value be 2 to n-1, n be source code in include space sum, to any character group: based on the first preset matching rule really The character that the fixed character group includes is character string or character, and obtains type based on the second preset matching rule.
Wherein, the first preset matching rule can be but not limited to the quantity for the character that character group includes, if character group Including character quantity be equal to 1, it is determined that the character group be character, if the quantity for the character that character group includes be greater than 1, Determine that the character group is character string, the second preset matching rule can be based on default reserved word, operator, punctuation mark sum number Word is matched, and for any one programming language, there is defined reserved word (i.e. above-mentioned default reserved word), operation Symbol, punctuation mark, it is possible thereby to type is determined based on these, and number is used for assignment generally in programming language, therefore Type can be determined based on number.Process is as follows: following steps are executed to any character group:
If the full content that the character group carries is one of number, operator, punctuation mark and default reserved word, Then the type of the character group is the corresponding type of full content, if character quantity determines the character group greater than 1 in the character group Determine that the character group is a character if character quantity is equal to 1 in the character group for a character string.
If the full content that the character group carries does not include number, operator, punctuation mark and default reserved word, it is somebody's turn to do The type of character group is argument types, if character quantity is greater than 1 in the character group, determines that the character group is a character String determines that the character group is a character if character quantity is equal to 1 in the character group.
If the number of characters in the character group is greater than 1 and including at least one preset characters, by preset characters to this Character group is split, and preset characters are determined as a character and the type of the character and the type of preset characters are identical, in advance If character includes: at least one of operator, punctuation mark and number.
To any portion in the various pieces split by preset characters: if character quantity is equal to 1 in the part, Then determine that the type that the part is a character and character is independent variable, if the character quantity in the part is greater than 1, it is determined that The part is a character string, if the character string is identical as one in default reserved word, it is determined that the type of the character string For reserved word type, otherwise determine that the type of the character string is argument types.
It should be noted that the underscore " _ " in punctuation mark in daily life, is not classified as punctuate in this application Underscore " _ " is classified as additional character, underscore and letter type having the same by the scope of symbol, the application, that is to say, that When one character string includes letter and underscore, do not need to split the character string.
If the character quantity in a character group is greater than 1 and including a preset characters, as included fortune in the character group Operator "=" then splits the character group by operator "=", and operator "=" is determined as a character and the word The type of symbol and the type of preset characters are identical.
If the character quantity in a character group is greater than 1 and including at least two preset characters, as wrapped in the character group Include operator "=" and punctuation mark " ", then by operator "=" and punctuation mark " " character group is split, will Operator "=" and punctuation mark " " it is identified as a character and the type of the character and the type of preset characters are identical i.e. Operator "=" is determined as operator type, determine punctuation mark " " for punctuation mark type.
If the character quantity in a character group is greater than 1 and including at least one preset characters, and at least one is default Include at least one number in character, then type decision is carried out to the previous character of the number, the previous word based on number The type of symbol is split.
A kind of feasible pattern that the type of previous character based on number is split is: if the previous word of number The type of symbol is letter, then cannot be split by number to the character group, is otherwise torn open by number to the character group Point.That is, number is followed behind letter, then letter and number is considered as an entirety, number is followed in operator or punctuate Behind symbol, then the number and operator or punctuation mark are split.
In order to make it easy to understand, above-mentioned partitioning scheme is illustrated herein by taking C language as an example:
Firstly, reserved word is the character group of defined mistake in C language, cannot be used again using these words as variable name, If " int " is the character group of defined mistake in C language, then " int " can only be used as reserved word, and cannot be as Independent variable uses, as programmer cannot use int when defining some title;Independent variable is the character of undefined mistake in C language Group, specifically programmer's self-defining character string or character.Such as " pwd_type ", the character string or character string of this definition are subsequent Attribute in use is by programmer's self-defining;Operator is for executing program code operation, and operator is to be arranged in C language Alright, wherein operator includes: unary operation, binocular operation and three mesh operations, and unary operation is that the operator only becomes one Amount is operated, such as "!" (logical NOT operator);Binocular operation is that the operator operates two variables, and such as "=" (assigns It is worth operator);Three mesh operations are that the operator operates three variables, such as "?: " (conditional operator), same operation Symbol and punctuation mark are also to set in C language, are no longer illustrated one by one to this present embodiment.
For example, it is desired to which the source code for carrying out Format adjusting is as follows:
First according to the first character of every row and the 1st space, the last character of every row and n-th of space, with And the two neighboring space of every row is split source code, obtained each character group be " viod ", " main () { ", " int ", " a ", "=", " 35 ", ";", " int ", " b=4;" " int ", " c " and "=a+b;", wherein between any two spaces Character be sky, then give up the character group.
Character group " 35 " is that all characters are number, it is determined that the character group is numeric type, and the character group Character quantity is greater than 1, determines that the character group is a character string.
Character group " viod ", " int ", " int " and " int " is identical as reserved word, it is determined that reserved word type, and should The character quantity of character group is greater than 1, determines that the character group is a character string.
Character group " a ", " c ", "=" and ";" character quantity be equal to 1, " a ", " c " are determined as letter, and "=" is determined as transporting Operator, ";" it is determined as punctuation mark, and determine that " a ", " c " are argument types, "=" is operator type, ";" be determined as marking Point symbol type, and the character quantity of the character group is equal to 1, determines that the character group is a character.
Character group " b=4;", " main () { " and "=a+b;" character quantity is greater than 1 and including preset characters, then to these Character group is split, by taking character group " main () { " as an example, fractionation obtain " main ", " () ", " ", then the character that will be obtained Group is determined type, determines type and implementation procedure and principle that above-mentioned code is determined type are just as no longer superfluous herein It states.
S103: being based on multiple independent corresponding types of component part, carries out Format adjusting to source code, is had The object code of specific format, wherein different source codes carries out the specific format between the object code obtained after Format adjusting It is identical.It is to be understood that the different corresponding types of independent component part, is opened up in a different format after being adjusted The independent component part shown, and the identical independent corresponding type of component part in different source codes, are adjusted it The independent component part of same format displaying is obtained afterwards, so that different source codes carries out the object code obtained after Format adjusting Between specific format it is identical.
Based on multiple independent corresponding types of component part, Format adjusting is carried out to source code, obtains having specific A kind of feasible pattern of the object code of format is: based on multiple independent corresponding types of component part and default adjustment rule Then, Format adjusting is carried out to source code, obtains the object code of specific format, so that different source codes is by default adjustment rule The specific format between object code obtained after then adjusting is identical.
Wherein presetting adjustment rule is the rule being defined for the format to source code, that is to say, that default adjustment Rule has defined that code is write using which type of format, so can show that final output by default adjustment rule The specific format of object code, default adjustment rule can be but not limited to: to every row of code, every class characters/character strings with And the call format of each section of the composition code such as header file, such as the default space of retraction first is required to every row, to every class Character or character string carry out space and separate, correct second default space of documentation requirements retraction, wherein first default space and Second default space may be the same or different, and the present embodiment a space default to first default space and second is not It limits, is not explained herein.
In the present embodiment, a kind of feasible pattern for the object code for obtaining specific format based on default adjustment rule is: Based on multiple independent corresponding types of component part, type adjustment rule corresponding with type are chosen from default adjustment rule Then, selection shares adjustment rule from default adjustment rule, shares and adjusts rule for forming in every line code except independent Part except part carries out format restriction, based on selected type adjustment rule and shares adjustment rule, to source code into Row format adjustment obtains the object code for having type adjustment rule and sharing format required by adjustment rule.
Wherein, it shares adjustment rule to be used to formatted restriction altogether in different code, such as limits the lattice of every line code The format of formula, header file, such as how much limit every line code retraction, whether header file allows to be retracted etc., at the same time it can also basis Different logical places of every line code in code, every line code has different specific formats, such as multistage nesting Its retraction is different, and code can be made to see on the whole methodically in this way, well arranged.Sharing adjustment rule may include But be not limited to: code retraction, redundancy null delete added with null, brace is aligned and every row only one program word;Type Adjustment rule is then the adjustment for certain type of character or character string, be can include but is not limited to: character or character string The space of front and back adjusts.
In the present embodiment, the retraction amplitude that code is retracted logical place phase in source code with corresponding program statement It closes, the retraction amplitude of the code retraction of the program statement of Different Logic position is different, such as and if this nested language of else For sentence, there is a situation where multistage nesting, then the retraction amplitude of every grade of nested code retraction used is different.Redundancy null is deleted Except with null addition can be adjusted according to desired specific format.
Here, be illustrated to the object code for obtaining specific format: source code is as follows by taking C language as an example:
The type of each independent component part of above-mentioned source code is obtained by above-mentioned steps S102, withing a hook at the end respectively is class Type, numeric type, punctuate type and pre- operator type.The corresponding class of these types can be extracted from default adjustment rule Type is adjusted rule combination and shares adjustment rule, is adjusted to source code, obtain following object code by type adjustment rule:
The rule being related to for obtaining above-mentioned object code is as follows: brace individually accounts for a line, a pair of of brace alignment, Hua Kuo Before and after being retracted four characters in number, deleting extra null, independent component part plus space, wherein independent component part front and back plus sky Lattice are as follows: separated between two characters/character strings of arbitrary neighborhood with space in every line code;Deleting extra null is arbitrary neighborhood Extra null is deleted between two line codes.
It should be noted that default adjustment rule can be rule as defined in the rule being artificially arranged in advance either system Then, new adjustment rule can also be increased according to demand under both forms, it is however noted that: no matter which kind of uses Default adjustment rule is adjusted, and needs to guarantee that the code that a project or a company are write has unified particular bin Formula.The rule being artificially arranged in advance be can be depending on the hobby of user, no longer illustrated herein.
According to the above-mentioned technical solution, the source code for needing to carry out code format adjustment is obtained, in the code of source code All characters in source code are split in compilation process, obtain multiple independent component parts, obtain multiple independent compositions The corresponding type in part is based on multiple independent corresponding types of component part, carries out Format adjusting to source code, obtains To the object code with specific format, wherein different source codes carries out the spy between the object code obtained after Format adjusting The formula that fixes is identical, realizes the source code for needing to carry out code format adjustment carrying out unified Format adjusting, to improve code Readability, and unified format can allow each programmer to be capable of the thinking and purpose of accurate understanding code, think in accurate understanding Code can accurately be changed on the basis of road and purpose, to be conducive to the modification and maintenance of code.And character point Cutting is carried out in the code compilation process of source code, can realize code compilation and code by the input of a character in this way It is carried out while Format adjusting, reduces the time-consuming of code compilation and code format adjustment.
For the various method embodiments described above, for simple description, therefore, it is stated as a series of action combinations, but Be those skilled in the art should understand that, the present invention is not limited by the sequence of acts described because according to the present invention, certain A little steps can be performed in other orders or simultaneously.Secondly, those skilled in the art should also know that, it is retouched in specification The embodiment stated belongs to preferred embodiment, and related actions and modules are not necessarily necessary for the present invention.
Corresponding with above method embodiment, the embodiment of the present invention also provides a kind of Format adjusting device, structural representation Figure is as shown in Fig. 2, may include: to obtain module 11, determination type module 12, adjustment module 13.
Obtain module 11, for obtaining the source code for needing to carry out code format adjustment, wherein the explanation of source code and Acquisition modes please refer to the related description in embodiment of the method, no longer illustrate this present embodiment.
Determination type module 12, for dividing in the code compilation process of source code all characters in source code It cuts, obtains multiple independent component parts, obtain multiple independent corresponding types of component part, wherein multiple independent compositions Any independent component part in part is character string or character.
That is for source code, which independently forms portion by character string and character both types It is grouped as, the stage that the present embodiment obtains multiple independent component parts is in code compilation process, such as in the word of code compilation The method analysis phase is analyzed this is because needing to input character one by one in the lexical analysis phase, and is obtaining independent composition portion Timesharing is also required to input character one by one, it is possible to be split in the lexical analysis phase, realize that a character input can obtain To multiple independent component parts and morphology is analyzed, so as to reduce character input number, improves code compilation and generation The time-consuming of code Format adjusting.
In the present embodiment, a kind of structure of determination type module 12 may is that determination type module 12 includes: to read list Member, cutting unit and type determining units.
Wherein reading unit, in the code compilation process of source code, every line character of source code to be inputted one by one; Cutting unit, the character group for including for obtaining every line character, wherein the character group that any one line character includes has: the line character Word in character group, the line character between middle first character and the 1st space between last character and n-th of space Character group between symbol group, i-th of space and i+1 space, the value of i are 2 to n-1, and n is the space for including in source code Sum;Type determining units, for any character group: determining the character that the character group includes based on the first preset matching rule For character string or character, and type is obtained based on the second preset matching rule.
Wherein, the first preset matching rule can be but not limited to the quantity for the character that character group includes, if character group Including character quantity be equal to 1, it is determined that the character group be character, if the quantity for the character that character group includes be greater than 1, Determine that the character group is character string, the second preset matching rule can be based on default reserved word, operator, punctuation mark sum number Word is matched, and for any one programming language, there is defined reserved word (i.e. above-mentioned default reserved word), operation Symbol, punctuation mark, it is possible thereby to type is determined based on these, and number is used for assignment generally in programming language, therefore Type can be determined based on number.Following steps are executed to determine type to any character group based on this type determination unit:
If the full content that the character group carries is one of number, operator, punctuation mark and default reserved word, Then the type of the character group is the corresponding type of full content, if character quantity determines the character group greater than 1 in the character group Determine that the character group is a character if character quantity is equal to 1 in the character group for a character string.
If the full content that the character group carries does not include number, operator, punctuation mark and default reserved word, it is somebody's turn to do The type of character group is argument types, if character quantity is greater than 1 in the character group, determines that the character group is a character String determines that the character group is a character if character quantity is equal to 1 in the character group.
If the number of characters in the character group is greater than 1 and including at least one preset characters, by preset characters to this Character group is split, and preset characters are determined as a character and the type of the character and the type of preset characters are identical, in advance If character includes: at least one of operator, punctuation mark and number.
To any portion in the various pieces split by preset characters: if character quantity is equal to 1 in the part, Then determine that the type that the part is a character and character is independent variable, if the character quantity in the part is greater than 1, it is determined that The part is a character string, if the character string is identical as one in default reserved word, it is determined that the type of the character string For reserved word type, otherwise determine that the type of the character string is argument types.
It should be noted that the underscore " _ " in punctuation mark in daily life, is not classified as punctuate in this application Underscore " _ " is classified as additional character, underscore and letter type having the same by the scope of symbol, the application, that is to say, that When one character string includes letter and underscore, do not need to split the character string.Wherein code division illustrates And obtained type please refers to the related description in embodiment of the method, no longer illustrates this present embodiment.
Module 13 is adjusted, for being based on multiple independent corresponding types of component part, format tune is carried out to source code It is whole, obtain the object code with specific format, wherein different source code carry out the object code obtained after Format adjusting it Between specific format it is identical.It is to be understood that the different corresponding types of independent component part, obtained after being adjusted with The independent component part that different formats is shown, and the identical independent corresponding class of component part in different source codes Type obtains the independent component part of same format displaying after being adjusted, so that after different source codes carries out Format adjusting The obtained specific format between object code is identical.
A kind of feasible pattern of object code for obtaining having specific format based on adjustment module 13 is: based on multiple independent groups At the corresponding type in part and default adjustment rule, Format adjusting is carried out to source code, obtains the target generation of specific format Code, so that the specific format between the object code that different source codes obtains after default adjustment rule adjustment is identical.
Wherein presetting adjustment rule is the rule being defined for the format to source code, that is to say, that default adjustment Rule has defined that code is write using which type of format, so can show that final output by default adjustment rule The specific format of object code, default adjustment rule can be but not limited to: to every row of code, every class characters/character strings with And each section call format of the composition code such as header file, such as the default space of retraction first is required to every row, to every class word Symbol or character string carry out space and separate, second default space of correct documentation requirements retraction, wherein first default space and the Two default spaces may be the same or different, and the present embodiment does not make first default space and second default space It limits, is not explained herein.
In the present embodiment, a kind of alternative construction for adjusting module 13 is: adjustment module 13 includes: that type adjustment rule is true Order member shares adjustment rule determination unit and adjustment unit.
Wherein type adjusts rule determination unit, for being based on multiple independent corresponding types of component part, from pre- If choosing type adjustment rule corresponding with type in adjustment rule;Adjustment rule determination unit is shared, is used for from default adjustment It is chosen in rule and shares adjustment rule, share adjustment rule for the part in every line code in addition to independent component part Carry out format restriction;Adjustment unit, for being carried out to source code based on selected type adjustment rule and shared adjustment rule Format adjusting obtains the object code for having type adjustment rule and sharing format required by adjustment rule.
Wherein, it shares adjustment rule to be used to formatted restriction altogether in different code, such as limits the lattice of every line code The format of formula, header file, such as how much limit every line code retraction, whether header file allows to be retracted etc., at the same time it can also basis Different logical places of every line code in code, every line code has different specific formats, such as multistage nesting Its retraction is different, and code can be made to see on the whole methodically in this way, well arranged.Sharing adjustment rule may include But be not limited to: code retraction, redundancy null delete added with null, brace is aligned and every row only one program word;Type Adjustment rule is then the adjustment for certain type of character or character string, be can include but is not limited to: character or character string The space of front and back adjusts.
In the present embodiment, the retraction amplitude that code is retracted logical place phase in source code with corresponding program statement It closes, the retraction amplitude of the code retraction of the program statement of Different Logic position is different, such as and if this nested language of else For sentence, there is a situation where multistage nesting, then the retraction amplitude of every grade of nested code retraction used is different.Redundancy null is deleted Except with null addition can be adjusted according to desired specific format.
Specific implementation procedure and principle in above method embodiment implementation procedure and principle it is identical, no longer go to live in the household of one's in-laws on getting married herein It states.
According to the above-mentioned technical solution, the source code for needing to carry out code format adjustment is obtained, in the code of source code All characters in source code are split in compilation process, obtain multiple independent component parts, obtain multiple independent compositions The corresponding type in part is based on multiple independent corresponding types of component part, carries out Format adjusting to source code, obtains To the object code with specific format, wherein different source codes carries out the spy between the object code obtained after Format adjusting The formula that fixes is identical, realizes the source code for needing to carry out code format adjustment carrying out unified Format adjusting, to improve code Readability, and unified format can allow each programmer to be capable of the thinking and purpose of accurate understanding code, think in accurate understanding Code can accurately be changed on the basis of road and purpose, to be conducive to the modification and maintenance of code.And character point Cutting is carried out in the code compilation process of source code, can realize code compilation and code by the input of a character in this way It is carried out while Format adjusting, reduces the time-consuming of code compilation and code format adjustment.
It should be noted that all the embodiments in this specification are described in a progressive manner, each embodiment weight Point explanation is the difference from other embodiments, and the same or similar parts between the embodiments can be referred to each other. For device class embodiment, since it is basically similar to the method embodiment, so being described relatively simple, related place ginseng See the part explanation of embodiment of the method.
Finally, it is to be noted that, herein, relational terms such as first and second and the like be used merely to by One entity or operation are distinguished with another entity or operation, without necessarily requiring or implying these entities or operation Between there are any actual relationship or orders.Moreover, the terms "include", "comprise" or its any other variant meaning Covering non-exclusive inclusion, so that the process, method, article or equipment for including a series of elements not only includes that A little elements, but also including other elements that are not explicitly listed, or further include for this process, method, article or The intrinsic element of equipment.In the absence of more restrictions, the element limited by sentence "including a ...", is not arranged Except there is also other identical elements in the process, method, article or apparatus that includes the element.
The foregoing description of the disclosed embodiments can be realized those skilled in the art or using the present invention.To this A variety of modifications of a little embodiments will be apparent for a person skilled in the art, and the general principles defined herein can Without departing from the spirit or scope of the present invention, to realize in other embodiments.Therefore, the present invention will not be limited It is formed on the embodiments shown herein, and is to fit to consistent with the principles and novel features disclosed in this article widest Range.
The above is only a preferred embodiment of the present invention, it is noted that for the ordinary skill people of the art For member, various improvements and modifications may be made without departing from the principle of the present invention, these improvements and modifications are also answered It is considered as protection scope of the present invention.

Claims (10)

1. a kind of Format adjusting method, which is characterized in that the described method includes:
Obtain the source code for needing to carry out code format adjustment;
All characters in the source code are split in the code compilation process of the source code, obtain multiple independences Component part obtains the multiple independent corresponding type of component part, any in the multiple independent component part Independent component part is character string or character;
Based on the multiple independent corresponding type of component part, Format adjusting is carried out to the source code, is had The object code of specific format, wherein different source codes carries out the specific format between the object code obtained after Format adjusting It is identical.
2. the method according to claim 1, wherein being based on the multiple independent corresponding class of component part Type carries out Format adjusting to the source code, obtains having the object code of specific format include:
Based on the multiple independent corresponding type of component part and default adjustment rule, format is carried out to the source code Adjustment, obtains the object code with specific format, so that different source codes obtained after default adjustment rule adjustment Specific format between object code is identical.
3. according to the method described in claim 2, it is characterized in that, described respectively corresponded to based on the multiple independent component part Type and default adjustment rule, Format adjusting is carried out to the source code, obtains having the object code of specific format include:
Based on the multiple independent corresponding type of component part, chosen from default adjustment rule corresponding with the type Type adjust rule;
Shared adjustment rule is chosen from the default adjustment rule, the shared adjustment rule is for only to removing in every line code Part except vertical component part carries out format restriction;
Based on selected type adjustment rule and adjustment rule is shared, Format adjusting is carried out to the source code, is had The type adjustment rule and the object code for sharing format required by adjustment rule.
4. the method according to claim 1, wherein it is described in the code compilation process of the source code to institute All characters stated in source code are split, and obtain multiple independent component parts, and it is each to obtain the multiple independent component part Self-corresponding type includes:
In the code compilation process of the source code, every line character of the source code is inputted one by one;
The character group that every line character includes is obtained, wherein the character group that any one line character includes has: first in the line character Character group in character group, the line character between character and the 1st space between last character and n-th of space, i-th Character group between a space and i+1 space, wherein the value of i is 2 to n-1, and n is the space for including in the source code Sum;
To any character group: determine that character that the character group includes is character string or character based on the first preset matching rule, And type is obtained based on the second preset matching rule.
5. according to the method described in claim 4, it is characterized in that, described to any character group: being advised based on the first preset matching It then determines that the character that the character group includes is character string or character, and type is obtained based on the second preset matching rule and includes:
Following steps are executed to any character group:
If the full content that the character group carries is one of number, operator, punctuation mark and default reserved word, it is somebody's turn to do The type of character group is the corresponding type of full content, if character quantity is greater than 1 in the character group, determines that the character group is one A character string determines that the character group is a character if character quantity is equal to 1 in the character group;
If the full content that the character group carries does not include number, operator, punctuation mark and default reserved word, the character The type of group is argument types, if character quantity is greater than 1 in the character group, determines that the character group is a character string, such as Character quantity is equal to 1 in the fruit character group, determines that the character group is a character;
If character quantity is greater than 1 and including at least one preset characters in the character group, by the preset characters to this Character group is split, and preset characters are determined as a character and the type of the character and the type of preset characters are identical, institute Stating preset characters includes at least one of operator, punctuation mark and number;
To any portion in each section split by the preset characters: if character quantity is equal to 1 in the part, Determine that the type that the part is a character and character is argument types, if character quantity is greater than 1 in the part, it is determined that The part is a character string, if the character string is identical as one in default reserved word, it is determined that the type of the character string For reserved word type, otherwise determine that the type of the character string is argument types.
6. a kind of Format adjusting device, which is characterized in that described device includes:
Module is obtained, for obtaining the source code for needing to carry out code format adjustment;
Determination type module, for being carried out in the code compilation process of the source code to all characters in the source code Segmentation obtains multiple independent component parts, obtains the multiple independent corresponding type of component part, the multiple independence Any independent component part in component part is character string or character;
Module is adjusted, for being based on the multiple independent corresponding type of component part, format is carried out to the source code Adjustment, obtains the object code with specific format, wherein different source codes carries out the object code obtained after Format adjusting Between specific format it is identical.
7. device according to claim 6, which is characterized in that the adjustment module, for being based on the multiple independent group At the corresponding type in part and default adjustment rule, Format adjusting is carried out to the source code, is obtained with specific format Object code so that the specific format between the object code that different source codes obtains after default adjustment rule adjustment It is identical.
8. device according to claim 6, which is characterized in that the adjustment module includes:
Type adjusts rule determination unit, for being based on the multiple independent corresponding type of component part, from default tune Type adjustment rule corresponding with the type is chosen in whole rule;
Adjustment rule determination unit is shared, shares adjustment rule, the shared tune for choosing from the default adjustment rule Whole rule is for carrying out format restriction to the part in every line code in addition to independent component part;
Adjustment unit, for carrying out format to the source code based on selected type adjustment rule and shared adjustment rule Adjustment obtains having the type adjustment rule and shares the object code of format required by adjustment rule.
9. device according to claim 6, which is characterized in that the determination type module includes:
Reading unit, in the code compilation process of the source code, every line character of the source code to be inputted one by one;
Cutting unit, the character group for including for obtaining every line character, wherein the character group that any one line character includes has: the row In character group, the line character in character between first character and the 1st space between last character and n-th of space Character group, the character group between i-th of space and i+1 space, wherein the value of i is 2 to arrive n-1, and n is the source code In include space sum;
Type determining units, for any character group: determining the character that the character group includes based on the first preset matching rule For character string or character, and type is obtained based on the second preset matching rule.
10. device according to claim 6, which is characterized in that the type determining units, for being held to any character group Row following steps:
If the full content that the character group carries is one of number, operator, punctuation mark and default reserved word, it is somebody's turn to do The type of character group is the corresponding type of full content, if character quantity is greater than 1 in the character group, determines that the character group is one A character string determines that the character group is a character if character quantity is equal to 1 in the character group;
If the full content that the character group carries does not include number, operator, punctuation mark and default reserved word, the character The type of group is argument types, if character quantity is greater than 1 in the character group, determines that the character group is a character string, such as Character quantity is equal to 1 in the fruit character group, determines that the character group is a character;
If character quantity is greater than 1 and including at least one preset characters in the character group, by the preset characters to this Character group is split, and preset characters are determined as a character and the type of the character and the type of preset characters are identical, institute Stating preset characters includes at least one of operator, punctuation mark and number;
To any portion in each section split by the preset characters: if character quantity is equal to 1 in the part, Determine that the type that the part is a character and character is argument types, if character quantity is greater than 1 in the part, it is determined that The part is a character string, if the character string is identical as one in default reserved word, it is determined that the type of the character string For reserved word type, otherwise determine that the type of the character string is argument types.
CN201811609730.6A 2018-12-27 2018-12-27 Format adjustment method and device Active CN109710304B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201811609730.6A CN109710304B (en) 2018-12-27 2018-12-27 Format adjustment method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201811609730.6A CN109710304B (en) 2018-12-27 2018-12-27 Format adjustment method and device

Publications (2)

Publication Number Publication Date
CN109710304A true CN109710304A (en) 2019-05-03
CN109710304B CN109710304B (en) 2022-06-24

Family

ID=66258551

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201811609730.6A Active CN109710304B (en) 2018-12-27 2018-12-27 Format adjustment method and device

Country Status (1)

Country Link
CN (1) CN109710304B (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112631606A (en) * 2020-12-31 2021-04-09 中国农业银行股份有限公司 Script formatting method and device
CN113220306A (en) * 2021-05-31 2021-08-06 支付宝(杭州)信息技术有限公司 Operation execution method and device and electronic equipment

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120154858A1 (en) * 2010-12-15 2012-06-21 Canon Kabushiki Kaisha Information processing apparatus and method of controlling the same
CN104050085A (en) * 2014-06-25 2014-09-17 北京思特奇信息技术股份有限公司 Forced code standard inspection method and system
CN104636320A (en) * 2015-01-29 2015-05-20 小米科技有限责任公司 Data processing method and device
CN107357733A (en) * 2017-07-17 2017-11-17 万帮充电设备有限公司 Improve the method and device of code quality
CN107515739A (en) * 2016-06-16 2017-12-26 阿里巴巴集团控股有限公司 Improve the method and device of code execution performance

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120154858A1 (en) * 2010-12-15 2012-06-21 Canon Kabushiki Kaisha Information processing apparatus and method of controlling the same
CN104050085A (en) * 2014-06-25 2014-09-17 北京思特奇信息技术股份有限公司 Forced code standard inspection method and system
CN104636320A (en) * 2015-01-29 2015-05-20 小米科技有限责任公司 Data processing method and device
CN107515739A (en) * 2016-06-16 2017-12-26 阿里巴巴集团控股有限公司 Improve the method and device of code execution performance
CN107357733A (en) * 2017-07-17 2017-11-17 万帮充电设备有限公司 Improve the method and device of code quality

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112631606A (en) * 2020-12-31 2021-04-09 中国农业银行股份有限公司 Script formatting method and device
CN113220306A (en) * 2021-05-31 2021-08-06 支付宝(杭州)信息技术有限公司 Operation execution method and device and electronic equipment

Also Published As

Publication number Publication date
CN109710304B (en) 2022-06-24

Similar Documents

Publication Publication Date Title
CN103473171B (en) A kind of fraction of coverage calling path based on function dynamically follows the tracks of method and device
CN105793818A (en) Source code translation
CN105069124A (en) Automatic ICD (International Classification of Diseases) coding method and system
US20060259506A1 (en) Method for automating software manufacturing process based on user interface form design, and computer readable medium recording computer executable instruction for performing the same
CN104679511A (en) Method for generating MapReduce codes based on MDE (Model-Driven Engineering) model transformation
CN110597842A (en) Service data query method and system
CN109683881A (en) A kind of code format method of adjustment and device
CN109710304A (en) A kind of Format adjusting method and device
CN101236493A (en) Program developing apparatus and program developing method
CN102193993A (en) Method, device and facility for determining similarity information between character string information
CN110162297A (en) A kind of source code fragment natural language description automatic generation method and system
Radošević et al. Source code generator based on dynamic frames
CN106599167A (en) System and method capable of supporting incremental upgrading of database
Yang et al. Identifying fragments to be extracted from long methods
US20130283233A1 (en) Multi-engine executable data-flow editor and translator
US20160018952A1 (en) Multi-Format Editors
US20090064092A1 (en) Visual programming language optimization
CN106776824A (en) The level method and system of Android databases
Regainia et al. A methodology of security pattern classification and of attack-defense tree generation
CN115713309A (en) Internal auditing system
Warnier Program modification
Van der Hoek Capturing product line architectures
US6981219B2 (en) Method and system for processing formulas and curves in a document
KR20060114569A (en) An operating methods for patent information system
CN112394926A (en) Code bed system based on natural language model

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant