Skip to content

Commit 68ae1ca

Browse files
author
Mikachu2333
committed
refactor(uv): 优化uv的函数复用
1 parent 749332c commit 68ae1ca

1 file changed

Lines changed: 20 additions & 75 deletions

File tree

  • src/recipe/lang/Python

src/recipe/lang/Python/uv.c

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -296,40 +296,6 @@ pl_python_uv_getsrc (char *option)
296296
}
297297

298298

299-
static bool
300-
pl_python_uv_path_is_pyproject (const char *path)
301-
{
302-
if (!path)
303-
return false;
304-
return xy_str_end_with (path, "pyproject.toml");
305-
}
306-
307-
308-
static char *
309-
pl_python_uv_replace_first_literal (const char *content, const char *old_literal, const char *new_literal)
310-
{
311-
if (!content || !old_literal || !new_literal)
312-
return NULL;
313-
314-
char *position = strstr (content, old_literal);
315-
if (!position)
316-
return NULL;
317-
318-
size_t prefix_len = position - content;
319-
size_t old_len = strlen (old_literal);
320-
size_t suffix_len = strlen (position + old_len);
321-
322-
size_t result_len = prefix_len + strlen (new_literal) + suffix_len;
323-
char *result = xy_malloc0 (result_len + 1);
324-
memcpy (result, content, prefix_len);
325-
memcpy (result + prefix_len, new_literal, strlen (new_literal));
326-
memcpy (result + prefix_len + strlen (new_literal), position + old_len, suffix_len);
327-
result[result_len] = '\0';
328-
329-
return result;
330-
}
331-
332-
333299
static char *
334300
pl_python_uv_replace_key_value (const char *content, const char *key, const char *new_value)
335301
{
@@ -478,37 +444,6 @@ pl_python_uv_insert_into_section (const char *content, const char *section_heade
478444
}
479445

480446

481-
static char *
482-
pl_python_uv_dirname (const char *path)
483-
{
484-
if (!path)
485-
return NULL;
486-
487-
const char *slash1 = strrchr (path, '/');
488-
const char *slash2 = strrchr (path, '\\');
489-
const char *slash = slash1 > slash2 ? slash1 : slash2;
490-
if (!slash)
491-
return NULL;
492-
493-
size_t len = slash - path + 1;
494-
char *dir = xy_malloc0 (len + 1);
495-
memcpy (dir, path, len);
496-
dir[len] = '\0';
497-
return dir;
498-
}
499-
500-
501-
static char *
502-
pl_python_uv_dup_normalized (const char *path)
503-
{
504-
if (!path)
505-
return NULL;
506-
507-
char *dup = xy_strdup (path);
508-
return xy_normalize_path (dup);
509-
}
510-
511-
512447
static void
513448
pl_python_uv_upsert_python (char **content,
514449
const char *existing_url,
@@ -521,15 +456,20 @@ pl_python_uv_upsert_python (char **content,
521456
{
522457
char *old_literal = xy_strcat (3, "\"", existing_url, "\"");
523458
char *new_literal = xy_strcat (3, "\"", new_url, "\"");
524-
char *replaced = pl_python_uv_replace_first_literal (*content, old_literal, new_literal);
459+
char *replaced = xy_str_gsub (*content, old_literal, new_literal);
460+
bool changed = !xy_streql (replaced, *content);
525461
free (old_literal);
526462
free (new_literal);
527-
if (replaced)
463+
if (changed)
528464
{
529465
free (*content);
530466
*content = replaced;
531467
updated = true;
532468
}
469+
else
470+
{
471+
free (replaced);
472+
}
533473
}
534474

535475
if (!updated)
@@ -601,15 +541,20 @@ pl_python_uv_upsert_pypi (char **content,
601541
{
602542
char *old_literal = xy_strcat (3, "\"", existing_url, "\"");
603543
char *new_literal = xy_strcat (3, "\"", new_url, "\"");
604-
char *replaced = pl_python_uv_replace_first_literal (*content, old_literal, new_literal);
544+
char *replaced = xy_str_gsub (*content, old_literal, new_literal);
545+
bool changed = !xy_streql (replaced, *content);
605546
free (old_literal);
606547
free (new_literal);
607-
if (replaced)
548+
if (changed)
608549
{
609550
free (*content);
610551
*content = replaced;
611552
updated = true;
612553
}
554+
else
555+
{
556+
free (replaced);
557+
}
613558
}
614559

615560
if (!updated)
@@ -739,7 +684,7 @@ pl_python_uv_target_add (PlPythonUvTarget targets[],
739684
{
740685
index = *count;
741686
targets[index].path = xy_strdup (path);
742-
targets[index].is_pyproject = pl_python_uv_path_is_pyproject (path);
687+
targets[index].is_pyproject = xy_str_end_with (path, "pyproject.toml");
743688
targets[index].update_python = update_python;
744689
targets[index].update_pypi = update_pypi;
745690
targets[index].existing_python_url = existing_python_url;
@@ -777,8 +722,8 @@ pl_python_uv_setsrc (char *option)
777722

778723
const char *PL_CPYTHON_URL = "https://mirror.nju.edu.cn/github-release/astral-sh/python-build-standalone/";
779724

780-
char *summary_python_path = pl_python_uv_dup_normalized (uv_config.python_path);
781-
char *summary_pypi_path = pl_python_uv_dup_normalized (uv_config.pypi_path);
725+
char *summary_python_path = uv_config.python_path ? xy_normalize_path (uv_config.python_path) : NULL;
726+
char *summary_pypi_path = uv_config.pypi_path ? xy_normalize_path (uv_config.pypi_path) : NULL;
782727

783728
char *uv_local_config_path = NULL;
784729
if (xy.on_windows)
@@ -791,8 +736,8 @@ pl_python_uv_setsrc (char *option)
791736
}
792737
uv_local_config_path = xy_normalize_path (uv_local_config_path);
793738

794-
char *proj_uv_path = pl_python_uv_dup_normalized (PL_Python_uv_Proj_uv);
795-
char *proj_pyproj_path = pl_python_uv_dup_normalized (PL_Python_uv_Proj_pyproj);
739+
char *proj_uv_path = xy_normalize_path (PL_Python_uv_Proj_uv);
740+
char *proj_pyproj_path = xy_normalize_path (PL_Python_uv_Proj_pyproj);
796741

797742
bool proj_uv_exist = xy_file_exist (proj_uv_path);
798743
bool proj_pyproj_exist = xy_file_exist (proj_pyproj_path);
@@ -870,7 +815,7 @@ pl_python_uv_setsrc (char *option)
870815
}
871816
else
872817
{
873-
char *dir = pl_python_uv_dirname (path);
818+
char *dir = xy_parent_dir (path);
874819
if (dir)
875820
{
876821
chsrc_ensure_dir (dir);

0 commit comments

Comments
 (0)