From 1c4d47c037c78cce4c3d5ad7502387ebfd4370cb Mon Sep 17 00:00:00 2001 From: Qu Wenruo Date: Tue, 9 Dec 2014 16:27:27 +0800 Subject: [PATCH] btrfs-progs: Add count_digits() function to help calculate filename len. Add count_digits() function in utils.h to help calculate filename with ino suffix. Signed-off-by: Qu Wenruo Signed-off-by: David Sterba --- utils.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/utils.h b/utils.h index 8950fca3..6cbb7b89 100644 --- a/utils.h +++ b/utils.h @@ -169,4 +169,22 @@ int find_next_key(struct btrfs_path *path, struct btrfs_key *key); char* btrfs_group_type_str(u64 flag); char* btrfs_group_profile_str(u64 flag); +/* + * Get the length of the string converted from a u64 number. + * + * Result is equal to log10(num) + 1, but without the use of math library. + */ +static inline int count_digits(u64 num) +{ + int ret = 0; + + if (num == 0) + return 1; + while (num > 0) { + ret++; + num /= 10; + } + return ret; +} + #endif