Created
December 1, 2009 09:15
-
-
Save tolmasky/246179 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
By default, a JavaScript method name is produced by concatenating the | |
components of an ObjectiveC method name, replacing ':' with '_', and | |
escaping '_' and '$' with a leading '$', such that '_' becomes "$_" and | |
'$' becomes "$$". For example: | |
ObjectiveC name Default JavaScript name | |
moveTo:: moveTo__ | |
moveTo_ moveTo$_ | |
moveTo$_ moveTo$$$_ | |
This function performs the inverse of that operation. | |
@result Fills 'buffer' with the ObjectiveC method name that corresponds to 'JSName'. | |
Returns true for success, false for failure. (Failure occurs when 'buffer' | |
is not big enough to hold the result.) | |
*/ | |
bool convertJSMethodNameToObjc(const char *JSName, char *buffer, size_t bufferSize) | |
{ | |
ASSERT(JSName && buffer); | |
const char *sp = JSName; // source pointer | |
char *dp = buffer; // destination pointer | |
char *end = buffer + bufferSize; | |
while (dp < end) { | |
if (*sp == '$') { | |
++sp; | |
*dp = *sp; | |
} else if (*sp == '_') | |
*dp = ':'; | |
else | |
*dp = *sp; | |
// If a future coder puts funny ++ operators above, we might write off the end | |
// of the buffer in the middle of this loop. Let's make sure to check for that. | |
ASSERT(dp < end); | |
if (*sp == 0) { // We finished converting JSName | |
ASSERT(strlen(JSName) < bufferSize); | |
return true; | |
} | |
++sp; | |
++dp; | |
} | |
return false; // We ran out of buffer before converting JSName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment