Skip to content

Instantly share code, notes, and snippets.

@tolmasky
Created December 1, 2009 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolmasky/246179 to your computer and use it in GitHub Desktop.
Save tolmasky/246179 to your computer and use it in GitHub Desktop.
/*
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