Fossil SCM

Move the fossil_strdup() implementation from import.c over to util.c where it belongs. Add a new fossil_strdup_nn() that mimics the behavior of mprintf("%s",...), only faster.

drh 2024-08-23 13:00 trunk
Commit 0c9dff644ec0bc70bb9ef3a34ff83b0331ddaab2ae80d5b0e06f60b214421721
3 files changed +6 -8 -22 +22
+6 -8
--- src/db.c
+++ src/db.c
@@ -896,11 +896,11 @@
896896
}
897897
int db_column_count(Stmt *pStmt){
898898
return sqlite3_column_count(pStmt->pStmt);
899899
}
900900
char *db_column_malloc(Stmt *pStmt, int N){
901
- return mprintf("%s", db_column_text(pStmt, N));
901
+ return fossil_strdup_nn(db_column_text(pStmt, N));
902902
}
903903
void db_column_blob(Stmt *pStmt, int N, Blob *pBlob){
904904
blob_append(pBlob, sqlite3_column_blob(pStmt->pStmt, N),
905905
sqlite3_column_bytes(pStmt->pStmt, N));
906906
}
@@ -1192,15 +1192,13 @@
11921192
char *z;
11931193
va_start(ap, zSql);
11941194
db_vprepare(&s, 0, zSql, ap);
11951195
va_end(ap);
11961196
if( db_step(&s)==SQLITE_ROW ){
1197
- z = mprintf("%s", sqlite3_column_text(s.pStmt, 0));
1198
- }else if( zDefault ){
1199
- z = mprintf("%s", zDefault);
1197
+ z = fossil_strdup_nn((const char*)sqlite3_column_text(s.pStmt, 0));
12001198
}else{
1201
- z = 0;
1199
+ z = fossil_strdup(zDefault);
12021200
}
12031201
db_finalize(&s);
12041202
return z;
12051203
}
12061204
@@ -2534,11 +2532,11 @@
25342532
if( isValidLocalDb(zPwd) ){
25352533
if( db_open_config(0, 1)==0 ){
25362534
return 0; /* Configuration could not be opened */
25372535
}
25382536
/* Found a valid check-out database file */
2539
- g.zLocalDbName = mprintf("%s", zPwd);
2537
+ g.zLocalDbName = fossil_strdup(zPwd);
25402538
zPwd[n] = 0;
25412539
while( n>0 && zPwd[n-1]=='/' ){
25422540
n--;
25432541
zPwd[n] = 0;
25442542
}
@@ -2670,11 +2668,11 @@
26702668
g.json.resultCode = FSL_JSON_E_DB_NOT_VALID;
26712669
#endif
26722670
fossil_fatal("not a valid repository: %s", zDbName);
26732671
}
26742672
}
2675
- g.zRepositoryName = mprintf("%s", zDbName);
2673
+ g.zRepositoryName = fossil_strdup(zDbName);
26762674
db_open_or_attach(g.zRepositoryName, "repository");
26772675
g.repositoryOpen = 1;
26782676
sqlite3_file_control(g.db, "repository", SQLITE_FCNTL_DATA_VERSION,
26792677
&g.iRepoDataVers);
26802678
@@ -3510,11 +3508,11 @@
35103508
zOut = db_text(0, "SELECT content FROM concealed WHERE hash=%Q", zKey);
35113509
}else{
35123510
zOut = 0;
35133511
}
35143512
if( zOut==0 ){
3515
- zOut = mprintf("%s", zKey);
3513
+ zOut = fossil_strdup_nn(zKey);
35163514
}
35173515
return zOut;
35183516
}
35193517
35203518
/*
35213519
--- src/db.c
+++ src/db.c
@@ -896,11 +896,11 @@
896 }
897 int db_column_count(Stmt *pStmt){
898 return sqlite3_column_count(pStmt->pStmt);
899 }
900 char *db_column_malloc(Stmt *pStmt, int N){
901 return mprintf("%s", db_column_text(pStmt, N));
902 }
903 void db_column_blob(Stmt *pStmt, int N, Blob *pBlob){
904 blob_append(pBlob, sqlite3_column_blob(pStmt->pStmt, N),
905 sqlite3_column_bytes(pStmt->pStmt, N));
906 }
@@ -1192,15 +1192,13 @@
1192 char *z;
1193 va_start(ap, zSql);
1194 db_vprepare(&s, 0, zSql, ap);
1195 va_end(ap);
1196 if( db_step(&s)==SQLITE_ROW ){
1197 z = mprintf("%s", sqlite3_column_text(s.pStmt, 0));
1198 }else if( zDefault ){
1199 z = mprintf("%s", zDefault);
1200 }else{
1201 z = 0;
1202 }
1203 db_finalize(&s);
1204 return z;
1205 }
1206
@@ -2534,11 +2532,11 @@
2534 if( isValidLocalDb(zPwd) ){
2535 if( db_open_config(0, 1)==0 ){
2536 return 0; /* Configuration could not be opened */
2537 }
2538 /* Found a valid check-out database file */
2539 g.zLocalDbName = mprintf("%s", zPwd);
2540 zPwd[n] = 0;
2541 while( n>0 && zPwd[n-1]=='/' ){
2542 n--;
2543 zPwd[n] = 0;
2544 }
@@ -2670,11 +2668,11 @@
2670 g.json.resultCode = FSL_JSON_E_DB_NOT_VALID;
2671 #endif
2672 fossil_fatal("not a valid repository: %s", zDbName);
2673 }
2674 }
2675 g.zRepositoryName = mprintf("%s", zDbName);
2676 db_open_or_attach(g.zRepositoryName, "repository");
2677 g.repositoryOpen = 1;
2678 sqlite3_file_control(g.db, "repository", SQLITE_FCNTL_DATA_VERSION,
2679 &g.iRepoDataVers);
2680
@@ -3510,11 +3508,11 @@
3510 zOut = db_text(0, "SELECT content FROM concealed WHERE hash=%Q", zKey);
3511 }else{
3512 zOut = 0;
3513 }
3514 if( zOut==0 ){
3515 zOut = mprintf("%s", zKey);
3516 }
3517 return zOut;
3518 }
3519
3520 /*
3521
--- src/db.c
+++ src/db.c
@@ -896,11 +896,11 @@
896 }
897 int db_column_count(Stmt *pStmt){
898 return sqlite3_column_count(pStmt->pStmt);
899 }
900 char *db_column_malloc(Stmt *pStmt, int N){
901 return fossil_strdup_nn(db_column_text(pStmt, N));
902 }
903 void db_column_blob(Stmt *pStmt, int N, Blob *pBlob){
904 blob_append(pBlob, sqlite3_column_blob(pStmt->pStmt, N),
905 sqlite3_column_bytes(pStmt->pStmt, N));
906 }
@@ -1192,15 +1192,13 @@
1192 char *z;
1193 va_start(ap, zSql);
1194 db_vprepare(&s, 0, zSql, ap);
1195 va_end(ap);
1196 if( db_step(&s)==SQLITE_ROW ){
1197 z = fossil_strdup_nn((const char*)sqlite3_column_text(s.pStmt, 0));
 
 
1198 }else{
1199 z = fossil_strdup(zDefault);
1200 }
1201 db_finalize(&s);
1202 return z;
1203 }
1204
@@ -2534,11 +2532,11 @@
2532 if( isValidLocalDb(zPwd) ){
2533 if( db_open_config(0, 1)==0 ){
2534 return 0; /* Configuration could not be opened */
2535 }
2536 /* Found a valid check-out database file */
2537 g.zLocalDbName = fossil_strdup(zPwd);
2538 zPwd[n] = 0;
2539 while( n>0 && zPwd[n-1]=='/' ){
2540 n--;
2541 zPwd[n] = 0;
2542 }
@@ -2670,11 +2668,11 @@
2668 g.json.resultCode = FSL_JSON_E_DB_NOT_VALID;
2669 #endif
2670 fossil_fatal("not a valid repository: %s", zDbName);
2671 }
2672 }
2673 g.zRepositoryName = fossil_strdup(zDbName);
2674 db_open_or_attach(g.zRepositoryName, "repository");
2675 g.repositoryOpen = 1;
2676 sqlite3_file_control(g.db, "repository", SQLITE_FCNTL_DATA_VERSION,
2677 &g.iRepoDataVers);
2678
@@ -3510,11 +3508,11 @@
3508 zOut = db_text(0, "SELECT content FROM concealed WHERE hash=%Q", zKey);
3509 }else{
3510 zOut = 0;
3511 }
3512 if( zOut==0 ){
3513 zOut = fossil_strdup_nn(zKey);
3514 }
3515 return zOut;
3516 }
3517
3518 /*
3519
-22
--- src/import.c
+++ src/import.c
@@ -73,32 +73,10 @@
7373
ImportFile *pInlineFile; /* File marked "inline" */
7474
int fromLoaded; /* True zFrom content loaded into aFile[] */
7575
int tagCommit; /* True if the commit adds a tag */
7676
} gg;
7777
78
-/*
79
-** Duplicate a string.
80
-*/
81
-char *fossil_strndup(const char *zOrig, int len){
82
- char *z = 0;
83
- if( zOrig ){
84
- int n;
85
- if( len<0 ){
86
- n = strlen(zOrig);
87
- }else{
88
- for( n=0; zOrig[n] && n<len; ++n );
89
- }
90
- z = fossil_malloc( n+1 );
91
- memcpy(z, zOrig, n);
92
- z[n] = 0;
93
- }
94
- return z;
95
-}
96
-char *fossil_strdup(const char *zOrig){
97
- return fossil_strndup(zOrig, -1);
98
-}
99
-
10078
/*
10179
** A no-op "xFinish" method
10280
*/
10381
static void finish_noop(void){}
10482
10583
--- src/import.c
+++ src/import.c
@@ -73,32 +73,10 @@
73 ImportFile *pInlineFile; /* File marked "inline" */
74 int fromLoaded; /* True zFrom content loaded into aFile[] */
75 int tagCommit; /* True if the commit adds a tag */
76 } gg;
77
78 /*
79 ** Duplicate a string.
80 */
81 char *fossil_strndup(const char *zOrig, int len){
82 char *z = 0;
83 if( zOrig ){
84 int n;
85 if( len<0 ){
86 n = strlen(zOrig);
87 }else{
88 for( n=0; zOrig[n] && n<len; ++n );
89 }
90 z = fossil_malloc( n+1 );
91 memcpy(z, zOrig, n);
92 z[n] = 0;
93 }
94 return z;
95 }
96 char *fossil_strdup(const char *zOrig){
97 return fossil_strndup(zOrig, -1);
98 }
99
100 /*
101 ** A no-op "xFinish" method
102 */
103 static void finish_noop(void){}
104
105
--- src/import.c
+++ src/import.c
@@ -73,32 +73,10 @@
73 ImportFile *pInlineFile; /* File marked "inline" */
74 int fromLoaded; /* True zFrom content loaded into aFile[] */
75 int tagCommit; /* True if the commit adds a tag */
76 } gg;
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78 /*
79 ** A no-op "xFinish" method
80 */
81 static void finish_noop(void){}
82
83
+22
--- src/util.c
+++ src/util.c
@@ -148,10 +148,32 @@
148148
}
149149
#else
150150
fossil_free(p);
151151
#endif
152152
}
153
+
154
+/*
155
+** Duplicate a string.
156
+*/
157
+char *fossil_strndup(const char *zOrig, ssize_t len){
158
+ char *z = 0;
159
+ if( zOrig ){
160
+ if( len<0 ) len = strlen(zOrig);
161
+ z = fossil_malloc( len+1 );
162
+ memcpy(z, zOrig, len);
163
+ z[len] = 0;
164
+ }
165
+ return z;
166
+}
167
+char *fossil_strdup(const char *zOrig){
168
+ return fossil_strndup(zOrig, -1);
169
+}
170
+char *fossil_strdup_nn(const char *zOrig){
171
+ if( zOrig==0 ) fossil_strndup("", 0);
172
+ return fossil_strndup(zOrig, -1);
173
+}
174
+
153175
154176
/*
155177
** Translate every upper-case character in the input string into
156178
** its equivalent lower-case.
157179
*/
158180
--- src/util.c
+++ src/util.c
@@ -148,10 +148,32 @@
148 }
149 #else
150 fossil_free(p);
151 #endif
152 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
154 /*
155 ** Translate every upper-case character in the input string into
156 ** its equivalent lower-case.
157 */
158
--- src/util.c
+++ src/util.c
@@ -148,10 +148,32 @@
148 }
149 #else
150 fossil_free(p);
151 #endif
152 }
153
154 /*
155 ** Duplicate a string.
156 */
157 char *fossil_strndup(const char *zOrig, ssize_t len){
158 char *z = 0;
159 if( zOrig ){
160 if( len<0 ) len = strlen(zOrig);
161 z = fossil_malloc( len+1 );
162 memcpy(z, zOrig, len);
163 z[len] = 0;
164 }
165 return z;
166 }
167 char *fossil_strdup(const char *zOrig){
168 return fossil_strndup(zOrig, -1);
169 }
170 char *fossil_strdup_nn(const char *zOrig){
171 if( zOrig==0 ) fossil_strndup("", 0);
172 return fossil_strndup(zOrig, -1);
173 }
174
175
176 /*
177 ** Translate every upper-case character in the input string into
178 ** its equivalent lower-case.
179 */
180

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button