From 9a71318c57229a7aa9668e777a4e3b4d29d1c677 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 9 Jun 2026 11:37:02 -0700 Subject: [PATCH 1/5] update: fortran in js --- dist/js/shared/str.d.ts | 2 +- dist/js/shared/str.js | 3 ++- src/js/shared/str.js | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/dist/js/shared/str.d.ts b/dist/js/shared/str.d.ts index bcda43c..d3f91f9 100644 --- a/dist/js/shared/str.d.ts +++ b/dist/js/shared/str.d.ts @@ -7,7 +7,7 @@ export function removeNewLinesAndExtraSpaces(str: any): any; export function randomAlphanumeric(length: number): string; export function toFixedLocale(number: any, precision: any): any; /** - * @summary Removes full-line and inline comments starting with #. Shebang (#!) is excluded. + * @summary Removes comments from a given source code text based on the specified programming language. * @param text {String} text to remove comments from. * @param language {String} programming language of the text. * @return {String} diff --git a/dist/js/shared/str.js b/dist/js/shared/str.js index a47b9db..5b2fde7 100644 --- a/dist/js/shared/str.js +++ b/dist/js/shared/str.js @@ -40,7 +40,7 @@ function toFixedLocale(number, precision) { } exports.toFixedLocale = toFixedLocale; /** - * @summary Removes full-line and inline comments starting with #. Shebang (#!) is excluded. + * @summary Removes comments from a given source code text based on the specified programming language. * @param text {String} text to remove comments from. * @param language {String} programming language of the text. * @return {String} @@ -48,6 +48,7 @@ exports.toFixedLocale = toFixedLocale; function removeCommentsFromSourceCode(text, language = "shell") { const regexList = { shell: /#(?!!).*$/gm, + fortran: /!.*$/gm, }; return text.replace(regexList[language], ""); } diff --git a/src/js/shared/str.js b/src/js/shared/str.js index b5dd7f9..5529114 100644 --- a/src/js/shared/str.js +++ b/src/js/shared/str.js @@ -37,7 +37,7 @@ export function toFixedLocale(number, precision) { } /** - * @summary Removes full-line and inline comments starting with #. Shebang (#!) is excluded. + * @summary Removes comments from a given source code text based on the specified programming language. * @param text {String} text to remove comments from. * @param language {String} programming language of the text. * @return {String} @@ -45,6 +45,7 @@ export function toFixedLocale(number, precision) { export function removeCommentsFromSourceCode(text, language = "shell") { const regexList = { shell: /#(?!!).*$/gm, + fortran: /!.*$/gm, }; return text.replace(regexList[language], ""); } From bc3dfb1191f35df1c16f14b4b81edc827650e8e2 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Tue, 9 Jun 2026 12:23:05 -0700 Subject: [PATCH 2/5] update: sync py and js --- dist/js/shared/str.js | 5 +++-- src/js/shared/str.js | 4 ++-- src/py/mat3ra/utils/string.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/dist/js/shared/str.js b/dist/js/shared/str.js index 5b2fde7..f8ebf52 100644 --- a/dist/js/shared/str.js +++ b/dist/js/shared/str.js @@ -46,11 +46,12 @@ exports.toFixedLocale = toFixedLocale; * @return {String} */ function removeCommentsFromSourceCode(text, language = "shell") { + var _a; const regexList = { shell: /#(?!!).*$/gm, - fortran: /!.*$/gm, + fortran: /[!#].*$/gm, }; - return text.replace(regexList[language], ""); + return text.replace((_a = regexList[language]) !== null && _a !== void 0 ? _a : regexList.shell, ""); } exports.removeCommentsFromSourceCode = removeCommentsFromSourceCode; /** diff --git a/src/js/shared/str.js b/src/js/shared/str.js index 5529114..6c1567d 100644 --- a/src/js/shared/str.js +++ b/src/js/shared/str.js @@ -45,9 +45,9 @@ export function toFixedLocale(number, precision) { export function removeCommentsFromSourceCode(text, language = "shell") { const regexList = { shell: /#(?!!).*$/gm, - fortran: /!.*$/gm, + fortran: /[!#].*$/gm, }; - return text.replace(regexList[language], ""); + return text.replace(regexList[language] ?? regexList.shell, ""); } /** diff --git a/src/py/mat3ra/utils/string.py b/src/py/mat3ra/utils/string.py index 5c68b75..56b9589 100644 --- a/src/py/mat3ra/utils/string.py +++ b/src/py/mat3ra/utils/string.py @@ -64,7 +64,7 @@ def remove_comments_from_source_code(text: str, language: str = "shell") -> str: TODO: consider preserving values enclosed in quotes """ if language == "fortran": - # Removes inline and full-line comments starting with ! or # + # Removes inline and full-line comments starting with ! or # (QE input uses both) return re.sub(r"[!#].*$", "", text, flags=re.MULTILINE) # Default (shell): removes inline and full-line # comments (except shebang) From 0f56847954c5a3ba7fb4340c6e1b5d4cbd8a86f2 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Wed, 10 Jun 2026 10:22:26 -0700 Subject: [PATCH 3/5] update: add python --- dist/js/shared/str.d.ts | 6 ++++++ dist/js/shared/str.js | 14 ++++++++++++-- src/js/shared/str.js | 12 +++++++++++- src/py/mat3ra/utils/__init__.py | 2 +- src/py/mat3ra/utils/string.py | 14 ++++++++++---- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/dist/js/shared/str.d.ts b/dist/js/shared/str.d.ts index d3f91f9..df37470 100644 --- a/dist/js/shared/str.d.ts +++ b/dist/js/shared/str.d.ts @@ -13,6 +13,12 @@ export function toFixedLocale(number: any, precision: any): any; * @return {String} */ export function removeCommentsFromSourceCode(text: string, language?: string): string; +/** + * @summary Removes comments from Quantum ESPRESSO input (Fortran ! then Python #). + * @param text {String} text to remove comments from. + * @return {String} + */ +export function removeCommentsQe(text: string): string; /** * @summary Removes empty lines from a given string. * @param string {String} string to remove empty lines from. diff --git a/dist/js/shared/str.js b/dist/js/shared/str.js index f8ebf52..07d18c6 100644 --- a/dist/js/shared/str.js +++ b/dist/js/shared/str.js @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.numberFormat = exports.numberPadArray = exports.numberPad = exports.createSafeFilename = exports.renderTemplateStringWithEval = exports.renderTemplateString = exports.findPreviousVersion = exports.convertArabicToRoman = exports.removeEmptyLinesFromString = exports.removeCommentsFromSourceCode = exports.toFixedLocale = exports.randomAlphanumeric = exports.removeNewLinesAndExtraSpaces = void 0; +exports.numberFormat = exports.numberPadArray = exports.numberPad = exports.createSafeFilename = exports.renderTemplateStringWithEval = exports.renderTemplateString = exports.findPreviousVersion = exports.convertArabicToRoman = exports.removeEmptyLinesFromString = exports.removeCommentsQe = exports.removeCommentsFromSourceCode = exports.toFixedLocale = exports.randomAlphanumeric = exports.removeNewLinesAndExtraSpaces = void 0; const coerce_1 = __importDefault(require("semver/functions/coerce")); const lt_1 = __importDefault(require("semver/functions/lt")); const rcompare_1 = __importDefault(require("semver/functions/rcompare")); @@ -49,11 +49,21 @@ function removeCommentsFromSourceCode(text, language = "shell") { var _a; const regexList = { shell: /#(?!!).*$/gm, - fortran: /[!#].*$/gm, + fortran: /!.*$/gm, + python: /#.*$/gm, }; return text.replace((_a = regexList[language]) !== null && _a !== void 0 ? _a : regexList.shell, ""); } exports.removeCommentsFromSourceCode = removeCommentsFromSourceCode; +/** + * @summary Removes comments from Quantum ESPRESSO input (Fortran ! then Python #). + * @param text {String} text to remove comments from. + * @return {String} + */ +function removeCommentsQe(text) { + return removeCommentsFromSourceCode(removeCommentsFromSourceCode(text, "fortran"), "python"); +} +exports.removeCommentsQe = removeCommentsQe; /** * @summary Removes empty lines from a given string. * @param string {String} string to remove empty lines from. diff --git a/src/js/shared/str.js b/src/js/shared/str.js index 6c1567d..dff9814 100644 --- a/src/js/shared/str.js +++ b/src/js/shared/str.js @@ -45,11 +45,21 @@ export function toFixedLocale(number, precision) { export function removeCommentsFromSourceCode(text, language = "shell") { const regexList = { shell: /#(?!!).*$/gm, - fortran: /[!#].*$/gm, + fortran: /!.*$/gm, + python: /#.*$/gm, }; return text.replace(regexList[language] ?? regexList.shell, ""); } +/** + * @summary Removes comments from Quantum ESPRESSO input (Fortran ! then Python #). + * @param text {String} text to remove comments from. + * @return {String} + */ +export function removeCommentsQe(text) { + return removeCommentsFromSourceCode(removeCommentsFromSourceCode(text, "fortran"), "python"); +} + /** * @summary Removes empty lines from a given string. * @param string {String} string to remove empty lines from. diff --git a/src/py/mat3ra/utils/__init__.py b/src/py/mat3ra/utils/__init__.py index 3446458..873aa32 100644 --- a/src/py/mat3ra/utils/__init__.py +++ b/src/py/mat3ra/utils/__init__.py @@ -2,4 +2,4 @@ from .string import camel_to_snake, snake_to_camel from .object import calculate_hash_from_object, remove_timestampable_keys, sort_keys_deep -from .string import remove_comments_from_source_code, remove_empty_lines_from_string +from .string import remove_comments_from_source_code, remove_comments_qe, remove_empty_lines_from_string diff --git a/src/py/mat3ra/utils/string.py b/src/py/mat3ra/utils/string.py index 56b9589..9bca372 100644 --- a/src/py/mat3ra/utils/string.py +++ b/src/py/mat3ra/utils/string.py @@ -64,13 +64,19 @@ def remove_comments_from_source_code(text: str, language: str = "shell") -> str: TODO: consider preserving values enclosed in quotes """ if language == "fortran": - # Removes inline and full-line comments starting with ! or # (QE input uses both) - return re.sub(r"[!#].*$", "", text, flags=re.MULTILINE) - - # Default (shell): removes inline and full-line # comments (except shebang) + return re.sub(r"!.*$", "", text, flags=re.MULTILINE) + if language == "python": + return re.sub(r"#.*$", "", text, flags=re.MULTILINE) return re.sub(r"#(?!!).*$", "", text, flags=re.MULTILINE) +def remove_comments_qe(text: str) -> str: + return remove_comments_from_source_code( + remove_comments_from_source_code(text, language="fortran"), + language="python", + ) + + def remove_empty_lines_from_string(text: str) -> str: """Removes empty lines and trims.""" return re.sub(r"^\s*[\r\n]", "", text, flags=re.MULTILINE).strip() From 35548a1c26ccc91c40ec7b029efe0bc06758a5ee Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Wed, 10 Jun 2026 10:24:03 -0700 Subject: [PATCH 4/5] chore: cleanup --- dist/js/shared/str.d.ts | 6 ------ dist/js/shared/str.js | 11 +---------- src/js/shared/str.js | 8 -------- src/py/mat3ra/utils/string.py | 7 ------- 4 files changed, 1 insertion(+), 31 deletions(-) diff --git a/dist/js/shared/str.d.ts b/dist/js/shared/str.d.ts index df37470..d3f91f9 100644 --- a/dist/js/shared/str.d.ts +++ b/dist/js/shared/str.d.ts @@ -13,12 +13,6 @@ export function toFixedLocale(number: any, precision: any): any; * @return {String} */ export function removeCommentsFromSourceCode(text: string, language?: string): string; -/** - * @summary Removes comments from Quantum ESPRESSO input (Fortran ! then Python #). - * @param text {String} text to remove comments from. - * @return {String} - */ -export function removeCommentsQe(text: string): string; /** * @summary Removes empty lines from a given string. * @param string {String} string to remove empty lines from. diff --git a/dist/js/shared/str.js b/dist/js/shared/str.js index 07d18c6..fdf859a 100644 --- a/dist/js/shared/str.js +++ b/dist/js/shared/str.js @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.numberFormat = exports.numberPadArray = exports.numberPad = exports.createSafeFilename = exports.renderTemplateStringWithEval = exports.renderTemplateString = exports.findPreviousVersion = exports.convertArabicToRoman = exports.removeEmptyLinesFromString = exports.removeCommentsQe = exports.removeCommentsFromSourceCode = exports.toFixedLocale = exports.randomAlphanumeric = exports.removeNewLinesAndExtraSpaces = void 0; +exports.numberFormat = exports.numberPadArray = exports.numberPad = exports.createSafeFilename = exports.renderTemplateStringWithEval = exports.renderTemplateString = exports.findPreviousVersion = exports.convertArabicToRoman = exports.removeEmptyLinesFromString = exports.removeCommentsFromSourceCode = exports.toFixedLocale = exports.randomAlphanumeric = exports.removeNewLinesAndExtraSpaces = void 0; const coerce_1 = __importDefault(require("semver/functions/coerce")); const lt_1 = __importDefault(require("semver/functions/lt")); const rcompare_1 = __importDefault(require("semver/functions/rcompare")); @@ -55,15 +55,6 @@ function removeCommentsFromSourceCode(text, language = "shell") { return text.replace((_a = regexList[language]) !== null && _a !== void 0 ? _a : regexList.shell, ""); } exports.removeCommentsFromSourceCode = removeCommentsFromSourceCode; -/** - * @summary Removes comments from Quantum ESPRESSO input (Fortran ! then Python #). - * @param text {String} text to remove comments from. - * @return {String} - */ -function removeCommentsQe(text) { - return removeCommentsFromSourceCode(removeCommentsFromSourceCode(text, "fortran"), "python"); -} -exports.removeCommentsQe = removeCommentsQe; /** * @summary Removes empty lines from a given string. * @param string {String} string to remove empty lines from. diff --git a/src/js/shared/str.js b/src/js/shared/str.js index dff9814..058b0a1 100644 --- a/src/js/shared/str.js +++ b/src/js/shared/str.js @@ -51,14 +51,6 @@ export function removeCommentsFromSourceCode(text, language = "shell") { return text.replace(regexList[language] ?? regexList.shell, ""); } -/** - * @summary Removes comments from Quantum ESPRESSO input (Fortran ! then Python #). - * @param text {String} text to remove comments from. - * @return {String} - */ -export function removeCommentsQe(text) { - return removeCommentsFromSourceCode(removeCommentsFromSourceCode(text, "fortran"), "python"); -} /** * @summary Removes empty lines from a given string. diff --git a/src/py/mat3ra/utils/string.py b/src/py/mat3ra/utils/string.py index 9bca372..6bb4ccd 100644 --- a/src/py/mat3ra/utils/string.py +++ b/src/py/mat3ra/utils/string.py @@ -70,13 +70,6 @@ def remove_comments_from_source_code(text: str, language: str = "shell") -> str: return re.sub(r"#(?!!).*$", "", text, flags=re.MULTILINE) -def remove_comments_qe(text: str) -> str: - return remove_comments_from_source_code( - remove_comments_from_source_code(text, language="fortran"), - language="python", - ) - - def remove_empty_lines_from_string(text: str) -> str: """Removes empty lines and trims.""" return re.sub(r"^\s*[\r\n]", "", text, flags=re.MULTILINE).strip() From 450ffb678389e638d0e8be1cdb4a5af1113b5cd2 Mon Sep 17 00:00:00 2001 From: VsevolodX Date: Wed, 10 Jun 2026 10:29:34 -0700 Subject: [PATCH 5/5] chore: cleanup --- src/py/mat3ra/utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/py/mat3ra/utils/__init__.py b/src/py/mat3ra/utils/__init__.py index 873aa32..3446458 100644 --- a/src/py/mat3ra/utils/__init__.py +++ b/src/py/mat3ra/utils/__init__.py @@ -2,4 +2,4 @@ from .string import camel_to_snake, snake_to_camel from .object import calculate_hash_from_object, remove_timestampable_keys, sort_keys_deep -from .string import remove_comments_from_source_code, remove_comments_qe, remove_empty_lines_from_string +from .string import remove_comments_from_source_code, remove_empty_lines_from_string