Introduce couch_db:normalize_dbname

Move duplicated logic into couch_db:normalize_dbname. We could use this
helper function anywhere we need to extract dbname from shard path.

COUCHDB-2715
diff --git a/src/couch_db.erl b/src/couch_db.erl
index ae80123..bbfbe0d 100644
--- a/src/couch_db.erl
+++ b/src/couch_db.erl
@@ -33,6 +33,7 @@
 -export([load_validation_funs/1]).
 -export([check_md5/2, with_stream/3]).
 -export([monitored_by/1]).
+-export([normalize_dbname/1]).
 
 -include_lib("couch/include/couch_db.hrl").
 
@@ -1469,3 +1470,8 @@
 
 select_lt(V1, V2) when V1 > V2 -> V2;
 select_lt(V1, _V2) -> V1.
+
+normalize_dbname(<<"shards/", _/binary>> = Path) ->
+    lists:last(binary:split(mem3:dbname(Path), <<"/">>, [global]));
+normalize_dbname(DbName) ->
+    DbName.
diff --git a/src/couch_server.erl b/src/couch_server.erl
index 77105ed..6c718ce 100644
--- a/src/couch_server.erl
+++ b/src/couch_server.erl
@@ -140,7 +140,7 @@
     end.
 
 path_ends_with(Path, Suffix) ->
-    Suffix == lists:last(binary:split(mem3:dbname(Path), <<"/">>, [global])).
+    Suffix == couch_db:normalize_dbname(Path).
 
 check_dbname(#server{dbname_regexp=RegExp}, DbName) ->
     case re:run(DbName, RegExp, [{capture, none}]) of