Skip to content

Commit

Permalink
Percent sign allowed as units, closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiker committed Mar 8, 2024
1 parent 9cbb316 commit b03fcdc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 4 additions & 2 deletions das2/units.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,13 @@ bool _Units_isSepByte(char c, char n){
}

bool _Units_isNameByte(char c, char n){
if( ((c>>7)&0x1) == 0 ) return isalpha(c); /* True if a ASCII 7-bit letter */
if( ((c>>6)&0x3) == 0x2 ) return true; /* True if UTF-8 continuation byte */
if( ((c>>7)&0x1) == 0 )
return ( isalpha(c) || ( c == '%') ); /* True if a ASCII 7-bit letter or % */
if( ((c>>6)&0x3) == 0x2 ) return true; /* True if UTF-8 continuation byte */

/* True if UTF-8 start byte and next byte is a continuation byte */
if( ( ((c>>6)&0x3) == 0x3 ) && ( ((n>>6)&0x3) == 0x2 ) ) return true;

return false;
}

Expand Down
17 changes: 15 additions & 2 deletions test/TestUnits.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main(int argc, char** argv) {

/* Exit on errors, log info messages and above */
das_init(argv[0], DASERR_DIS_EXIT, 0, DASLOG_INFO, NULL);

/* Test singleton nature of unit values */
das_units Hz1 = Units_fromStr("Hz");
char sHz2[20] = {'\0'}; strcpy(sHz2, "Hz");
Expand Down Expand Up @@ -296,7 +296,20 @@ int main(int argc, char** argv) {
das_units num_dens1 = Units_fromStr(sUnits);
das_units num_dens2 = Units_fromStr("electrons cm**-3");
if( num_dens1 != num_dens2){
printf("ERROR: Test 33 Failed, %s != %s", num_dens1, num_dens2);
printf("ERROR: Test 33 Failed, %s != %s\n", num_dens1, num_dens2);
return 15;
}

/* Odd one inspired by Tracers/Magic, milli percent per meter*/
das_units milli_percent = Units_fromStr("m%");
das_units meter = Units_fromStr("m");
das_units milli_per2 = Units_multiply(milli_percent, Units_invert(meter));

rFactor = 0.0;
das_units milli_per3 = Units_reduce( Units_fromStr("milli%/m"), &rFactor);

if(rFactor != 0.001 ){
printf("ERROR: Test 34 Failed, %s is not 1/1000 of %s\n", milli_per2, milli_per3);
return 15;
}

Expand Down

0 comments on commit b03fcdc

Please sign in to comment.