如何按位比较的String [英] How to Bitwise compare a String

查看:178
本文介绍了如何按位比较的String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是需要一系列的权限字符串,少于255个字符,并将它们分配到的实体的功能。分配每个字符串是唯一的,但有这么多的拖放到一个数组,序列化他们推到一个数据库,后来拉出来和反序列化他们或从查询每次重新计算有一个负载已造成延误问题。特别是与继承的权限。

I am working on a function that takes in a series of permission strings, less than 255 characters and assigns them to an entity. Each string assigned is unique, but there are so many that dropping them into an array, serializing them and pushing into a database and pulling them out later and de-serializing them or recalculating from a query every time there is a load has been causing delay problems. Especially with inherited permissions.

所以我想带串,从中产生屏蔽,然后中用OR进入权限水珠。随着越来越多的权限添加继续或他们的水珠。然后,当你需要验证许可,反对水珠字符串。

So I was thinking of taking the string, generating a mask from it then OR'ing into the permissions glob. As more permissions are added continue to OR them to the glob. Then when you need to verify the permission AND the string against the glob.

的问题是如何生成的掩模。起初我想只是散列串一个独特的面具,但它是可以想象的,但我不知道该怎么可能,让我们从更多的散列值或运算到水珠的潜力,在这样的填充水珠方式,并和测试用的许可,他们没有,还没有返回真值。

The question is how to generate the mask. At first I was thinking of just hashing the string for a unique mask, however it is conceivable, but I don't know how likely, that as more hash values are OR'ed onto the glob the potential for filling the glob in such a way that and AND test with a permission they don't have, yet return a true value.

if($glob&&$test == $test)

另一种选择将只是自动编号许可字符串和有自己的面具为2 ^自动编号。但是,这将允许串的数量限制为各地64ish。

The other option would be just auto-number the permission strings and have their mask be 2^auto-number. But that would limit the number of permission strings to around 64ish.

我真正想要的是某种我可以拉出一个数据库的一次,它与用户关联的水珠。然后测试对一个字符串,水珠或相关的转口货值presenting权限集。

What I really want is a glob of some sort that I can pull out of a database once and associate it with the user. Then test that glob against a string or associated value representing a permission set.

推荐答案

我发现了一个有趣的解决方案,但我不能确定,如果逻辑上是如何正确的,因为我不是很熟悉PHP如何处理字符串数据。我决定切掉一切,尝试做直没有任何散列或分配或诸如此类的东西,只是做字符串位运算。它似乎工作,但我不知道我可以证明我的逻辑真就好了。

I found an interesting solution but I am unsure if how logically correct it is because I am not very familiar with how PHP handles string data. I decided to cut away everything and try to do it straight without any hashing or assigning or whatnot and just do bitwise operations on strings. It seemed to work, but I am not sure I can prove my logic true enough.

$key1 = "Access to Black Box";
$key2 = "Managing Black Box";
$key3 = "Nothing too see here";
$key3a = "Nothingg B";
$key3b = "too see";
$glob = "";

$glob = $glob | $key1;
if(($glob & $key1) == $key1){echo "<p>Key one exists in glob: " . $glob;}

$glob = $glob | $key2;
if(($glob & $key2) == $key2){echo "<p>Key one exists in glob: " . $glob;}

if(($glob & $key3) == $key3){echo "<p>Key three exists in glob: " . $glob;}
else{echo "<p>Key three does not exists in glob: " . $glob;}

$glob = $glob | $key3;
if(($glob & $key3) == $key3){echo "<p>Key three exists in glob: " . $glob;}

if(($glob & $key3a) == $key3a){echo "<p>Key three a exists in glob: " . $glob;}

if(($glob & $key3b) == $key3b){echo "<p>Key three b exists in glob: " . $glob;} 
else{echo "<p>Key three b does not exists in glob: " . $glob;}

输出:


Key one exists in glob: Access to Black Box
Key two exists in glob: Mcoew{nwobnmckkbox
Key three does not exists in glob: Mcoew{nwobnmckkbox
Key three exists in glob: Oomowoomsooboze
Key three a exists in glob: Oomowoomsooboze
Key three b does not exists in glob: Oomowoomsooboze

所以这个工作,但我会怎么会在碰撞智慧看?随着key3a我发现有匹配与其他键的字符位置的字符组合的字符串,我可以得到一个假阳性。但我可以避开它与许可字符串严格的规定?每个资源类型被命名和每个资源类型具有相关联的权限的数量有限。因此,像博客....写邮报,博客......发表邮报,博客....中等邮报,播客上传.......,播客.... ...发布,以弥补碰撞的概率增大,因为字符串的长度对PHP的速度影响不大。

So this works, but what would I be looking at collision wise? With key3a I showed that a string that has a combination of characters that match positions with characters in other keys I can get a false positive. But can I get around it with strict rules on the permission strings? Each resource type is named and each resource type has a limited number of associated permissions. So something like "Blog....Write Post", "Blog...Publish Post", "Blog....Moderate Post", "Podcast.......Upload", "Podcast.......Publish" to compensate for the increasing probability of a collision since string length has little impact on PHP's speed.

这篇关于如何按位比较的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆