Skip to content

Commit 4e3e7b6

Browse files
committed
GStr clear fix?
1 parent d5fa21b commit 4e3e7b6

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

GStr.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ GStr::Data* GStr::new_data(const char* str, uint addcap) {
5050

5151
void GStr::prep_data(uint len, uint addcap) {
5252

53-
if (len+addcap == my_data->cap && my_data->ref_count <= 1)
53+
if (len+addcap <= my_data->cap && my_data->ref_count <= 1) {
54+
my_data->length=len;
55+
my_data->chars[len]=0;
5456
return;
55-
57+
}
5658
if (my_data != &null_data && --my_data->ref_count == 0)
5759
GFREE(my_data);
5860

@@ -67,6 +69,14 @@ void GStr::prep_data(uint len, uint addcap) {
6769
my_data = &null_data;
6870
}
6971

72+
GStr& GStr::clear(int init_cap) {
73+
make_unique(); //edit operation ahead
74+
75+
prep_data(0, init_cap);
76+
return *this;
77+
}
78+
79+
7080
void GStr::replace_data(Data *data) {
7181
if (my_data != &null_data && --my_data->ref_count == 0)
7282
GFREE(my_data);
@@ -215,7 +225,7 @@ GStr& GStr::operator=(const char *s) {
215225
if (s==NULL) {
216226
prep_data(0);
217227
return *this;
218-
}
228+
}
219229
const int len = ::strlen(s); prep_data(len);
220230
::memcpy(my_data->chars, s, len);
221231
return *this;
@@ -343,12 +353,6 @@ GStr GStr::copy() const {
343353
return newstring;
344354
}
345355

346-
GStr& GStr::clear() {
347-
make_unique(); //edit operation ahead
348-
prep_data(0);
349-
return *this;
350-
}
351-
352356
int GStr::index(const GStr& s, int start_index) const {
353357
return index(s.chars(), start_index);
354358
}

GStr.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class GStr {
104104

105105
GStr& upper();
106106
GStr& lower();
107-
GStr& clear();//make empty
107+
GStr& clear(int init_cap=0);//make empty, but can specify initial capacity
108108
//character translation or removal:
109109
GStr& tr(const char* from, const char* to=NULL);
110110
//number of occurences of a char in the string:
@@ -184,10 +184,11 @@ class GStr {
184184
};
185185
static Data* new_data(uint len, uint addcap=0); //alloc a specified length string's Data
186186
static Data* new_data(const char* str, uint addcap=0); //alloc a copy of a specified string, with an additional cap
187-
void prep_data(uint len, uint addcap=0); //allocates memory for the string
187+
void prep_data(uint len, uint addcap=0); //allocates memory for the string, if needed
188188
void replace_data(Data* data);
189+
//WARNING (dangerous): direct access to pointer; string editing cannot change the length!
190+
char* chrs();
189191
void make_unique();
190-
char* chrs(); // this is dangerous, length should not be affected
191192
static Data null_data; //a null (empty) string Data is available here
192193
Data* my_data; //pointer to a Data object holding actual string data
193194
};
@@ -203,7 +204,7 @@ inline const char *GStr::chars() const {
203204
return my_data->chars;
204205
}
205206

206-
inline char *GStr::chrs() { //protected version, allows modification of the chars
207+
inline char *GStr::chrs() { //allows direct modification of the chars !
207208
return my_data->chars;
208209
}
209210

codons.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ unsigned short packCodon(char n1, char n2, char n3) {
5151
byte b1=n1-'A';
5252
byte b2=n2-'A';
5353
byte b3=n3-'A';
54-
b1 |= (b2 << 5);
54+
b1 |= (b2 << 5);
5555
b2 = (b2 >> 3) | (b3 << 2);
5656
return ( ((unsigned short)b2) << 8) + b1;
5757
}
@@ -62,7 +62,7 @@ bool codonTableInit() {
6262
for (int i=0;i<cdsize;i+=4) {
6363
unsigned short aacode=packCodon(codonData[i], codonData[i+1], codonData[i+2]);
6464
codonTable[aacode]=codonData[i+3];
65-
}
65+
}
6666
return true;
6767
}
6868

@@ -88,3 +88,9 @@ char* translateDNA(const char* dnastr, int& aalen, int dnalen) {
8888
}
8989
return r;
9090
}
91+
92+
char translateCodon(const char* dna) { //returns the aminoacid code for the 1st codon at dna
93+
if (dna==NULL) return 0;
94+
if (dna[0]==0 || dna[1]==0 || dna[2]==0) return 0;
95+
return codonTable[packCodon(toupper(dna[0]),toupper(dna[1]),toupper(dna[2]))];
96+
}

codons.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ struct Codon {
2626
nuc[1]=toupper(s2);
2727
nuc[2]=toupper(s3);
2828
}
29-
30-
29+
30+
3131
char& operator[](int idx) {
32-
if (idx<0 || idx>2)
32+
if (idx<0 || idx>2)
3333
GError("Error: Codon index out of bounds!\n");
3434
return nuc[idx];
3535
}
3636

3737
char operator[](int idx) const {
38-
if (idx<0 || idx>2)
38+
if (idx<0 || idx>2)
3939
GError("Error: Codon index out of bounds!\n");
4040
return nuc[idx];
4141
}
42-
42+
4343
char translate();
4444
};
4545

@@ -48,7 +48,8 @@ struct Codon {
4848
// responsible for freeing the returned string!
4949
char* translateDNA(const char* dnastr, int& aalen, int dnalen=0);
5050

51+
char translateCodon(const char* dna); //returns the aminoacid code for the 1st codon at dna
5152

5253
bool codonTableInit();
53-
54+
5455
#endif

gdna.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
const char* IUPAC_2BIT ="AACCTTGGTTAAAAAACCCCGGAAAAAACCAAAAAA";
55
const char* IUPAC_2BITN ="001133223300000011112200000011000000";
6-
const char* IUPAC_DEFS ="AaCcTtGgUuMmRrWwSsYyKkVvHhDdBbNnXx-*";
7-
const char* IUPAC_COMP ="TtGgAaCcAaKkYyWwSsRrMmBbDdHhVvNnXx-*";
6+
const char* IUPAC_DEFS ="AaCcTtGgUuMmRrWwSsYyKkVvHhDdBbNnXx-*";
7+
const char* IUPAC_COMP ="TtGgAaCcAaKkYyWwSsRrMmBbDdHhVvNnXx-*";
88

99
#define A_2BIT 0 // 00
1010
#define C_2BIT 1 // 01

gdna.h

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define GDNA_H
33
#include "GBase.h"
44

5+
extern const char* IUPAC_DEFS;
6+
extern const char* IUPAC_COMP;
7+
58
char ntComplement(char c);
69

710
//in-place reverse complement of a nucleotide (sub)sequence

0 commit comments

Comments
 (0)