Repository: Tencent/MMKV
File: Android/MMKV/mmkv/src/main/cpp/MMKV.cpp (C++)

Code before:
    } else {
        return false;
    }
}

static void mkSpecialCharacterFileDirectory() {
    char *path = strdup((g_rootDir + "/" + SPECIAL_CHARACTER_DIRECTORY_NAME).c_str());
    mkPath(path);
    free(path);
}

static void md5(string &value) {
    unsigned char md[MD5_DIGEST_LENGTH];
    char tmp[3] = {'\0'}, buf[33] = {'\0'};
    MD5((const unsigned char *)value.c_str(), value.size(), md);
    for(int i = 0;i < MD5_DIGEST_LENGTH;i++)
    {
        sprintf(tmp, "%2.2x", md[i]);
        strcat(buf, tmp);
    }
    value = buf;
}

static string encodeFilePath(const string &mmapID) {
	const char *specialCharacters = "\\/:*?\"<>|";
	string filePath(mmapID);
	bool hasSpecialCharacter = false;
	for (int i = 0; i < filePath.size(); i++) {
		if (strchr(specialCharacters, filePath[i]) != NULL) {
            md5(filePath);
			hasSpecialCharacter = true;
			break;
		}
	}
	if (hasSpecialCharacter) {
		static pthread_once_t once_control = PTHREAD_ONCE_INIT;
		pthread_once(&once_control, mkSpecialCharacterFileDirectory);
		return SPECIAL_CHARACTER_DIRECTORY_NAME + "/" + filePath;
	}
	return filePath;
}

static string mappedKVPathWithID(const string &mmapID, MMKVMode mode) {
    return (mode & MMKV_ASHMEM) == 0 ? g_rootDir + "/" + encodeFilePath(mmapID)
                                     : string(ASHMEM_NAME_DEF) + "/" + encodeFilePath(mmapID);
}

static string crcPathWithID(const string &mmapID, MMKVMode mode) {
    return (mode & MMKV_ASHMEM) == 0 ? g_rootDir + "/" + encodeFilePath(mmapID) + ".crc" : encodeFilePath(mmapID) + ".crc";
}


Review comment (suggestion):
This string copy is unnecessary. I suggest making `void md5(string&)` into `string md5(const string&)`. And `filePath = md5(mmapID)`

Code after:
    } else {
        return false;
    }
}

static void mkSpecialCharacterFileDirectory() {
    char *path = strdup((g_rootDir + "/" + SPECIAL_CHARACTER_DIRECTORY_NAME).c_str());
    mkPath(path);
    free(path);
}

static string md5(const string &value) {
    unsigned char md[MD5_DIGEST_LENGTH];
    char tmp[3] = {'\0'}, buf[33] = {'\0'};
    MD5((const unsigned char *)value.c_str(), value.size(), md);
    for(int i = 0;i < MD5_DIGEST_LENGTH;i++)
    {
        sprintf(tmp, "%2.2x", md[i]);
        strcat(buf, tmp);
    }
    return buf;
}

static string encodeFilePath(const string &mmapID) {
    const char *specialCharacters = "\\/:*?\"<>|";
    string filePath;
    bool hasSpecialCharacter = false;
    for (int i = 0; i < filePath.size(); i++) {
        if (strchr(specialCharacters, filePath[i]) != NULL) {
            filePath = md5(mmapID);
            hasSpecialCharacter = true;
            break;
        }
    }
    if (hasSpecialCharacter) {
        static pthread_once_t once_control = PTHREAD_ONCE_INIT;
        pthread_once(&once_control, mkSpecialCharacterFileDirectory);
        return SPECIAL_CHARACTER_DIRECTORY_NAME + "/" + filePath;
    }
    else {
        return mmapID;
    }
    return filePath;
}

static string mappedKVPathWithID(const string &mmapID, MMKVMode mode) {
    return (mode & MMKV_ASHMEM) == 0 ? g_rootDir + "/" + encodeFilePath(mmapID)
                                     : string(ASHMEM_NAME_DEF) + "/" + encodeFilePath(mmapID);
}

static string crcPathWithID(const string &mmapID, MMKVMode mode) {