清理 CPU 名称#3928
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a standardized way to clean and normalize CPU model names across platforms, adds a whitespace-normalization utility, and covers both with unit tests.
- Add
CentralProcessor.cleanNameto strip extraneous tokens from CPU names. - Update Windows and Linux detectors to apply
cleanNamebefore setting the CPU name. - Introduce
StringUtils.normalizeWhitespacesand corresponding tests.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/CentralProcessor.java | Add cleanName method to normalize CPU names |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsCPUDetector.java | Apply cleanName in Windows CPU detection |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/linux/LinuxCPUDetector.java | Apply cleanName in Linux CPU detection |
| HMCLCore/src/main/java/org/jackhuang/hmcl/util/StringUtils.java | Add normalizeWhitespaces utility |
| HMCLCore/src/test/java/org/jackhuang/hmcl/util/platform/hardware/CentralProcessorTest.java | Add tests for cleanName covering various CPU strings |
| HMCLCore/src/test/java/org/jackhuang/hmcl/util/StringUtilsTest.java | Add tests for whitespace normalization |
There was a problem hiding this comment.
Pull Request Overview
This PR adds a cleanName utility to normalize CPU model strings, integrates it into Windows/Linux detectors, and provides tests for both CPU name cleaning and whitespace normalization.
- Introduce
CentralProcessor.cleanNameto strip extraneous suffixes and trademarks - Update Windows and Linux detectors to call
cleanNamewhen setting CPU names - Add JUnit tests for
cleanNameandStringUtils.normalizeWhitespaces
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| CentralProcessor.java | New cleanName method to sanitize raw CPU name strings |
| WindowsCPUDetector.java | Apply cleanName when setting builder.name |
| LinuxCPUDetector.java | Apply cleanName and handle additional “Model name” key |
| StringUtils.java | New normalizeWhitespaces to trim/collapse whitespace |
| CentralProcessorTest.java | Tests covering various CPU name inputs |
| StringUtilsTest.java | Tests covering whitespace normalization |
Comments suppressed due to low confidence (2)
HMCLCore/src/test/java/org/jackhuang/hmcl/util/platform/hardware/CentralProcessorTest.java:31
- There’s no test asserting how
cleanNamehandles anullinput. Consider addingassertNull(cleanName(null))or similar to codify expected behavior.
public void testCleanName() {
HMCLCore/src/test/java/org/jackhuang/hmcl/util/StringUtilsTest.java:30
- Add a test for
normalizeWhitespaces(null)to verify that passingnullreturns an empty string as implemented.
public void testNormalizeWhitespaces() {
| * @author Glavo | ||
| */ | ||
| public final class CentralProcessor { | ||
|
|
There was a problem hiding this comment.
The cleanName method lacks a JavaDoc explaining its normalization rules and null behavior. Consider adding a description of input/output expectations and examples.
| /** | |
| * Normalizes a processor name by removing unnecessary details and standardizing the format. | |
| * <p> | |
| * This method handles specific patterns for Intel, AMD, and Loongson processors, removing | |
| * redundant information such as core counts, trademarks, and other suffixes. If the input | |
| * is {@code null}, the method returns {@code null}. | |
| * </p> | |
| * | |
| * <p><b>Examples:</b></p> | |
| * <ul> | |
| * <li>Input: {@code "Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz"} → Output: {@code "Intel Core i7-9700K"}</li> | |
| * <li>Input: {@code "AMD Ryzen 5 3600 6-Core Processor"} → Output: {@code "AMD Ryzen 5 3600"}</li> | |
| * <li>Input: {@code "Loongson-3A R4 (Loongson-3A3000)"} → Output: {@code "Loongson-3A3000"}</li> | |
| * <li>Input: {@code null} → Output: {@code null}</li> | |
| * </ul> | |
| * | |
| * @param name the original processor name, or {@code null} | |
| * @return the normalized processor name, or {@code null} if the input is {@code null} | |
| */ |
|
|
||
| public static String cleanName(String name) { | ||
| if (name == null) | ||
| return null; |
There was a problem hiding this comment.
cleanName returns null when input is null but its signature doesn’t reflect that. Consider annotating the method with @Nullable or returning an empty string for consistency with normalizeWhitespaces.
| return null; | |
| return ""; |
No description provided.