Skip to content

fix: maidata部分字段开头如果有空白字符时,会造成一些解析和处理过程(如add1Bar函数)出现错误。#47

Merged
clansty merged 1 commit intoMuNET-OSS:mainfrom
Starrah:fix-maidata-trim
Mar 19, 2026
Merged

fix: maidata部分字段开头如果有空白字符时,会造成一些解析和处理过程(如add1Bar函数)出现错误。#47
clansty merged 1 commit intoMuNET-OSS:mainfrom
Starrah:fix-maidata-trim

Conversation

@Starrah
Copy link
Copy Markdown
Contributor

@Starrah Starrah commented Mar 19, 2026

出问题谱面节选:

&lv_5=14
&des_5=強欲で金満な壺(kms & 415 with 小狼白夜 a.k.a. Spectrum 11 VS 八雲ちぐさ)    
&inote_5=
(180){8}5/6h[8:11],4,,4,,3,,2,
,3,,4,{16}6/5,,,2/3,,,{8}8/7,
2/7h[8:11],2,,2,,2,,2,

这时,在

var kvps = new SimaiFile(stream).ToKeyValuePairs();
var maiData = new Dictionary<string, string>();
foreach (var (key, value) in kvps)
{
maiData[key] = value;
}

所解析出的maidata会形如[inote_5] = "\r\n(180){8}5/6h[8:11],4",开头有一个\r\n

这就导致add1Bar里所调用的

[GeneratedRegex(@"^\([\d\.]+\)")]
private static partial Regex BpmTagRegex();

这里的正则表达式无法匹配到tag了,因为它限定“要从谱面文本开头开始匹配”,但开头是个\r\n

从根本性解决问题,最好是把所有谱面i_note的开头结尾都trim掉,这样是比较保险的。至于为什么只trim inote lv first等字段,不trim title artist之类的,想想如月车站(歌名为日文全角空格\u3000)就知道了。

Summary by Sourcery

错误修复:

  • 通过去除 maidatainotelvfirstwholebpm 键值周围的空白字符,对这些值进行规范化处理,从而确保基于正则表达式的标签解析能够正常工作。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Normalize maidata values for inote, lv, first, and wholebpm keys by trimming surrounding whitespace so regex-based tag parsing works correctly.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在增强MaiChartManager处理maidata的健壮性。通过在数据导入阶段对特定关键字段进行空白字符修剪,解决了因数据源中意外的空白字符导致后续解析逻辑(特别是正则表达式匹配)失败的问题,从而提升了谱面数据处理的准确性和稳定性。

Highlights

  • 修复maidata解析错误: 解决了maidata中部分字段(如inote、lv、first、wholebpm)开头存在空白字符时,导致正则表达式匹配失败,进而引发add1Bar等函数处理错误的问题。
  • 增加字段空白字符修剪: 在导入maidata时,对inote、lv、first和wholebpm这些关键字段的值进行了Trim()操作,移除了开头和结尾的空白字符,确保数据格式的规范性。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Mar 19, 2026

审阅者指南(在小型 PR 上折叠)

审阅者指南

在导入过程中,通过去除前后空白字符来规范化某些 maidata 字段,这样即使字段以换行或空格开头,下游解析(例如 add1Bar 的正则标签匹配)也能正确工作。

带裁剪字段的 maidata 导入与下游解析时序图

sequenceDiagram
    participant Caller
    participant MaidataImportService
    participant RegexTagParser

    Caller->>MaidataImportService: ImportMaidata(rawMaidataText)
    MaidataImportService->>MaidataImportService: Parse keyValuePairs from raw text
    loop For each keyValuePair
        MaidataImportService->>MaidataImportService: maiData[key] = value
        alt Key is inote*, lv*, first*, wholebpm*
            MaidataImportService->>MaidataImportService: maiData[key] = maiData[key].Trim()
        else Other keys
            MaidataImportService->>MaidataImportService: Store value without trimming
        end
    end
    MaidataImportService-->>Caller: ImportChartResult with normalized maiData

    Caller->>MaidataImportService: add1Bar(maiData, chartIndex)
    MaidataImportService->>RegexTagParser: Match tags from maiData[inote_key]
    RegexTagParser-->>MaidataImportService: Successful tag matches (no leading whitespace)
    MaidataImportService-->>Caller: Updated chart with added bar
Loading

MaidataImportService 导入与规范化逻辑的更新类图

classDiagram
    class MaidataImportService {
        +ImportChartResult ImportMaidata(string fileName, string rawMaidataText)
        -Dictionary~string,string~ ParseKeyValuePairs(string rawMaidataText)
        -void add1Bar(Dictionary~string,string~ maiData, int chartIndex)
    }

    class ImportChartResult {
        +bool Success
        +string Message
        +Dictionary~int,AllChartsEntry~ AllCharts
    }

    class AllChartsEntry {
        +int ChartIndex
        +string InoteKey
        +string LevelKey
        +string FirstKey
        +string WholeBpmKey
    }

    MaidataImportService --> ImportChartResult : returns
    ImportChartResult "1" --> "*" AllChartsEntry : contains

    note for MaidataImportService "ImportMaidata now trims values for keys starting with inote, lv, first and wholebpm before storing them in maiData"
Loading

文件级更改

更改 细节 文件
在导入时规范化特定 maidata 字段,以避免当值包含前后空白字符时解析失败。
  • 在从解析出的键值对填充 maiData 字典之后,添加一个条件逻辑:对以 inote、lv、first 或 wholebpm 开头的键,对其值进行 Trim。
  • 确保只有这些技术性字段会被去除空白,保留如 title/artist 等可能合法以空白字符开头的字段。
MaiChartManager/Services/MaidataImportService.cs

技巧与命令

与 Sourcery 交互

  • 触发新审阅: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 通过回复某条审阅评论,让 Sourcery 从该评论创建一个 issue。你也可以在审阅评论下回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title,随时(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在你想要的位置随时生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary,随时(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。若你已经处理完所有评论且不想再看到它们,这会很有用。
  • 忽略所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有 Sourcery 审阅。特别适合在你想从头开始新的审阅时使用——别忘了再评论 @sourcery-ai review 以触发新的审阅!

自定义你的体验

访问你的控制面板以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的 pull request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Normalizes certain maidata fields by trimming leading/trailing whitespace during import so that downstream parsing (e.g., add1Bar’s regex tag matching) works correctly even when fields start with line breaks or spaces.

Sequence diagram for maidata import and downstream parsing with trimmed fields

sequenceDiagram
    participant Caller
    participant MaidataImportService
    participant RegexTagParser

    Caller->>MaidataImportService: ImportMaidata(rawMaidataText)
    MaidataImportService->>MaidataImportService: Parse keyValuePairs from raw text
    loop For each keyValuePair
        MaidataImportService->>MaidataImportService: maiData[key] = value
        alt Key is inote*, lv*, first*, wholebpm*
            MaidataImportService->>MaidataImportService: maiData[key] = maiData[key].Trim()
        else Other keys
            MaidataImportService->>MaidataImportService: Store value without trimming
        end
    end
    MaidataImportService-->>Caller: ImportChartResult with normalized maiData

    Caller->>MaidataImportService: add1Bar(maiData, chartIndex)
    MaidataImportService->>RegexTagParser: Match tags from maiData[inote_key]
    RegexTagParser-->>MaidataImportService: Successful tag matches (no leading whitespace)
    MaidataImportService-->>Caller: Updated chart with added bar
Loading

Updated class diagram for MaidataImportService import and normalization logic

classDiagram
    class MaidataImportService {
        +ImportChartResult ImportMaidata(string fileName, string rawMaidataText)
        -Dictionary~string,string~ ParseKeyValuePairs(string rawMaidataText)
        -void add1Bar(Dictionary~string,string~ maiData, int chartIndex)
    }

    class ImportChartResult {
        +bool Success
        +string Message
        +Dictionary~int,AllChartsEntry~ AllCharts
    }

    class AllChartsEntry {
        +int ChartIndex
        +string InoteKey
        +string LevelKey
        +string FirstKey
        +string WholeBpmKey
    }

    MaidataImportService --> ImportChartResult : returns
    ImportChartResult "1" --> "*" AllChartsEntry : contains

    note for MaidataImportService "ImportMaidata now trims values for keys starting with inote, lv, first and wholebpm before storing them in maiData"
Loading

File-Level Changes

Change Details Files
Normalize specific maidata fields on import to avoid parsing failures when values contain leading/trailing whitespace.
  • After populating the maiData dictionary from parsed key–value pairs, add a conditional that trims the value for keys starting with inote, lv, first, or wholebpm.
  • Ensure that only these technical fields are whitespace-trimmed, preserving fields like title/artist that may legitimately begin with spacing characters.
MaiChartManager/Services/MaidataImportService.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给出了一些高层次的反馈:

  • 当前的裁剪(trim)逻辑被重复执行了:先通过 maiData[key] = value; 赋值,然后又进行第二次赋值;可以考虑在插入字典之前先对值进行 trim,或者使用一个局部变量来避免重复的查找和写入。
  • 在内联条件中对键前缀(inote, lv, first, wholebpm)进行硬编码,使这一规则有些不够直观,也更难维护;可以考虑将这组前缀,或者 ShouldTrimKey(string key) 这样的谓词提取到单独的方法或配置中,这样会更清晰,也更便于扩展。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- The trimming logic is currently duplicated via `maiData[key] = value;` followed by a second assignment; consider trimming the value before inserting into the dictionary or using a local variable to avoid redundant lookups and writes.
- Hardcoding the key prefixes (`inote`, `lv`, `first`, `wholebpm`) in an inline conditional makes the rule a bit opaque and harder to maintain; consider extracting the prefix set or the `ShouldTrimKey(string key)` predicate into a separate method or config so it’s clearer and easier to extend.

Sourcery 对开源项目免费 —— 如果你觉得我们的代码审查有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的代码审查。
Original comment in English

Hey - I've left some high level feedback:

  • The trimming logic is currently duplicated via maiData[key] = value; followed by a second assignment; consider trimming the value before inserting into the dictionary or using a local variable to avoid redundant lookups and writes.
  • Hardcoding the key prefixes (inote, lv, first, wholebpm) in an inline conditional makes the rule a bit opaque and harder to maintain; consider extracting the prefix set or the ShouldTrimKey(string key) predicate into a separate method or config so it’s clearer and easier to extend.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The trimming logic is currently duplicated via `maiData[key] = value;` followed by a second assignment; consider trimming the value before inserting into the dictionary or using a local variable to avoid redundant lookups and writes.
- Hardcoding the key prefixes (`inote`, `lv`, `first`, `wholebpm`) in an inline conditional makes the rule a bit opaque and harder to maintain; consider extracting the prefix set or the `ShouldTrimKey(string key)` predicate into a separate method or config so it’s clearer and easier to extend.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个PR修复了maidata中部分字段开头存在空白字符时导致的解析错误。通过对inote, lv, first, wholebpm等字段的值进行Trim()操作,确保了后续处理(如add1Bar函数中的正则表达式匹配)的正确性。代码改动直接有效。我有一个小建议,可以优化代码,避免对字典进行不必要的重复写入,从而提高一点点性能和代码清晰度。

Comment on lines 276 to +278
maiData[key] = value;
if (key.StartsWith("inote") || key.StartsWith("lv") || key.StartsWith("first") ||
key.StartsWith("wholebpm")) maiData[key] = maiData[key].Trim();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这里的实现虽然能正常工作,但会存在对字典的重复写入问题。当 if 条件满足时,会先执行 maiData[key] = value;,然后再执行 maiData[key] = maiData[key].Trim();,对同一个键写入了两次。

为了避免这种情况,我们可以重构一下,根据条件只写入一次。这样代码会更高效和清晰。

            if (key.StartsWith("inote") || key.StartsWith("lv") || key.StartsWith("first") ||
                key.StartsWith("wholebpm"))
            {
                maiData[key] = value.Trim();
            }
            else
            {
                maiData[key] = value;
            }

@clansty clansty merged commit 10f63d1 into MuNET-OSS:main Mar 19, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants