Skip to content

feat: introduce a new replace template syntax ${*} - #98

Open
logic-finder wants to merge 2 commits into
mainfrom
feat/wildcard-replace-template
Open

feat: introduce a new replace template syntax ${*}#98
logic-finder wants to merge 2 commits into
mainfrom
feat/wildcard-replace-template

Conversation

@logic-finder

Copy link
Copy Markdown
Contributor

${*} resolves to the arbitrary sections in rt.db. Incidentally, it is valid to write like ${*|user|char}.

closes #72.

@logic-finder
logic-finder requested a review from lshqqytiger July 25, 2026 10:06
@logic-finder logic-finder self-assigned this Jul 25, 2026
@logic-finder logic-finder added the enhancement New feature or request label Jul 25, 2026
@lshqqytiger
lshqqytiger requested a review from Copilot July 26, 2026 06:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Introduces a new replace-template token ${*} for the C hhss implementation so templates can resolve to a randomly chosen section in rt.db (and supports mixed forms like ${*|user|char}).

Changes:

  • Add wildcard section selection in replace_templates() and route to the existing user/non-user replacement handlers.
  • Introduce rtdbquerybyidx() to query rt.db sections by index and add section-name constants (SECTNAME_*) in global.h.
  • Prevent [*] from being used as a literal section name in rt.db parsing.

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
hhss/dat.db Updates sample data to exercise the new ${*} template.
hhss/c/rtdbparse.h Exposes rtdbquerybyidx() for index-based section lookup.
hhss/c/rtdbparse.c Implements rtdbquerybyidx() and disallows * as a section name.
hhss/c/replace.internals.h Includes global.h for section-name constants used by replacement logic.
hhss/c/replace.c Implements ${*} wildcard replacement behavior over rt.db sections.
hhss/c/global.h Adds SECTNAME_WILDCARD / SECTNAME_USER macros for consistent section naming.

Comment thread hhss/c/replace.c
Comment on lines +34 to +36
if (STREQL(rt, SECTNAME_WILDCARD)) { /* wildcard template? */
v = rand_range(0, array_size(rtdb) - 1);
sectarr = rtdbquerybyidx(rtdb, v, &sectname);

@logic-finder logic-finder Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

rtdbcheck(), which is called before replace_templates(), ensures that rt.db shouldn't be empty. Therefore, I don't think we need to validate the emptiness once more here.

Comment thread hhss/c/rtdbparse.c
Comment on lines +45 to +49
extern array_t *rtdbquerybyidx(array_t *table, size_t idx, const char **sectname) {
mapper_t *mapper = array_get(table, idx);
*sectname = mapper->sect;
return mapper->addr;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

rtdbquerybyidx, as its name implies, expects idx be [0, rtdb.len - 1]. So I don't want to bother checking out-of-range errors. That's so cumbersome.

Comment thread hhss/c/rtdbparse.c
Comment on lines 140 to +144
l->run[rearpos] = '\0';
return l->run + frontpos;

if (STREQL(ret, SECTNAME_WILDCARD)) {
reason = "section name can't be *";
synerr();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

intended for convenience

Comment thread hhss/c/replace.c
Comment on lines +34 to +35
if (STREQL(rt, SECTNAME_WILDCARD)) { /* wildcard template? */
v = rand_range(0, array_size(rtdb) - 1);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replace templates other than ${user} is currently a feature only supported by the C implementation.

@logic-finder

Copy link
Copy Markdown
Contributor Author

done

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

hhss/c/rtdbparse.c:67

  • rtdbcheck() does not currently reject an empty rt.db (dbsiz==0), so wildcard replacement (${*}) can still reach rand_range(0, array_size(rtdb) - 1) with an empty table and fail with an internal range error. Add an explicit empty-db validation in rtdbcheck() so the failure is reported clearly and before template replacement runs.
      sectname = m->sect;
      sectarr_siz = array_size(sectarr);

      if (STREQL(sectname, SECTNAME_USER)) {
         if (sectarr_siz >= threshold)

Comment thread hhss/c/rtdbparse.c
Comment on lines +45 to +47
extern array_t *rtdbquerybyidx(array_t *table, size_t idx, const char **sectname) {
mapper_t *mapper = array_get(table, idx);
if (sectname) *sectname = mapper->sect;
Comment thread hhss/c/rtdbparse.h
Comment on lines 13 to 16
array_t *rtdbparse(array_t *linestr);
array_t *rtdbquery(array_t *table, const char *sectname);
array_t *rtdbquerybyidx(array_t *table, size_t idx, const char **sectname);
void rtdbcheck(array_t *db);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

replace template 신규문법 ${*}

2 participants